Comp 170-201 -- Dordal/Nabicht -- Lab 3, Wed, Sept 11, 2002 1. Write a program to find the sum of the squares 1^2 + 2^2 + ... + 1000^2. Use a for loop or a while loop. Hint: the answer is 333833500. 2. Write a program to print "Floyd's Triangle": 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ... Print the first 15 rows. Can you figure out how to get the columns to line up neatly? Or, at least, more neatly than I managed? (Hint: convert the number to a string, and just handle three cases for 1-digit, 2-digit, and 3-digit numbers: if (str.length() == 1) ... else if (str.length() == 2) ... else ... ) (You don't have to *do* the neat alignment; just figure out how.) 3. Both the above can be written as for loops. Consider Kakutani's Problem: take a number N and: if N is even, N = N/2 if N is odd, N = 3*N +1 See how long (if ever) it takes to get to 1. For example, if N=30, the sequence of N's is 30, 15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1 Write a loop to implement Kakutani's problem. I did all these in a single program. ==================================================== Here are some supplemental questions. You should submit *written* answers to these, and also the three programs above. You can, if you want, type the answers into a big comment in your program. Q1. Make your program 1 above into an infinite loop. Explain how you did this, and how you were able to halt the running program. Q2. Put a println statement into the body of the loop of program 1 above. Describe how this might help debugging. Q3. Leave off a loop-closing "}" from the first program. What error message do you get? Q4. Why do you need a while loop rather than a for loop for the Kakutani program? Q5. What happens if you run the Kakutani program starting with N=27? How long toes it toake to get to 1? Q6. Put a semicolon right after the (condition) of a while loop; for example: while (N != 1); {...} Does it compile or run? What happens?