14-Section-Handout

14-Section-Handout - CS106A Handout 14 Spring 2011 Section...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106A Handout 14 Spring 2011 April 11 th , 2011 Section Handout Discussion Problem 1: Precedence (Chapter 3, exercise 6, page 92) In Norton Juster’s The Phantom Tollbooth , the Mathemagician gives Milo the following problem to solve: 4+9 -2* 16+1/3*6 - 67+8*2 -3+ 26 -1/ 34+3/7+2 - 5 According to Milo’s calculations, which are corroborated by the Mathemagician, this expression "all works out to zero." If you do the calculation, however, the expression comes out to zero only if you start at the beginning and apply all the operators in strict left- to-right order. What would the answer be if the Mathemagician’s expression were evaluated using Java’s precedence rules? Discussion Problem 2: Perfect and Near Perfect Numbers Write a console program using for loops and if tests to print out all perfect and near perfect numbers between 1 and 10000. A number is considered perfect if it happens to be equal to the sum of its proper divisors. For instance, 28 is a perfect number, because 1, 2, 4, 7, and 14 all divide evenly into 28, and 1 + 2 + 4 + 7 + 14 equals 28. A near perfect number is one that is one or two away from its proper divisor sum. The number 20 is near perfect, because 1, 2, 4, 5, and 10 all divide evenly into 20, and 1 + 2 + 4 + 5 + 10 is 22, which is within 2 of 20. The output of your program should consistent with the following: This program prints all of the perfect numbers between 1 and 10000 1 is near perfect. 2 is near perfect. 3 is near perfect.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

14-Section-Handout - CS106A Handout 14 Spring 2011 Section...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online