created 01/01/03
Rewrite the TriangleTester program described in the chapter so
that the user is asked for N and that the value entered is
tested to make sure it is positive.
Click here to go back to the main menu.
Rewrite TriangleTester so that Triangle()
is a static method of that class.
Eliminate the TriangleCalc class.
(Include user input from exercise 1, if you want.)
Click here to go back to the main menu.
Write a program that implements this definition of square numbers:
square(1) = 1 square(N) = square(N-1) + 2N -1
Make a complete program similar to TriangleTester.java given in the chapter.
Aside: where did this crazy definition of square come from?
Easy: this is just algebra:
(N-1)2 = N2 - 2N + 1rearrange to get:
N2 = (N-1)2 + 2N - 1
Another way to think of this is a square number give the total number of pins in a square arrangement of pins with N pins on a side. Draw a square with four dots (pins) on a side. Then expand the square so that it has five dots on a side. Using your picture, explain why the recursive definition of square numbers is correct.
Square numbers are also mentioned in the quiz for this chapter.
Click here to go back to the main menu.
Write a program that implements this definition of
log for positive integers N:
log(1) = 1 log(N) = 1 + log(N/2)
Use only integer math for this.
Click here to go back to the main menu.