Recursion on the Rocks

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

You stop applying the hammer when each sub-problem (rock fragment) is small enough.

Recursion on the Rocks

Here (again) are the two parts to recursion:

  1. If the problem is easy, solve it immediately.
  2. If the problem can't be solved immediately, divide it into smaller problems, then:
    • Solve the smaller problems.

Here are how this applies to the rock-destruction problem:

  1. When a piece is small, don't pound it any further.
  2. To destroy a large rock, hit it with a hammer. The rock shatters into smaller pieces.
    • Apply this procedure to each of the pieces.

QUESTION 6:

Say that you wish to divide a line into 16 equal pieces.

What is the first thing that you do?