Professional Documents
Culture Documents
School Yearbook Instructions
School Yearbook Instructions
XXXX
XXXX
XXXX
Math tutorial
• If the number of photos is N, then in order to make a rectangular arrangement, a number x
≤ N must be a factor of N.
o This means you are searching for factors of N in this program.
o This means x y = N, where x and y are always whole numbers (int), and thus factors of N.
• For the most efficient code, you need not go all the way to N to search for factors. In fact,
you only need to go as high as ⌊ √N ⌋ (the square root of N, rounded down), counting from
1.
import math
max = math.floor(math.sqrt(N)); // the square root of N, rounded down
and know that you have found all of the possible factors.
• If a factor is found, that is because its remainder is zero in division: (N % x) == 0 for some
integer x ≤ N. If this conditional is true, then x is a valid dimension of the photograph
arrangement.
• If x ≥ ⌊ √N ⌋, then you can assume no factors exist and that N is prime.
• Once a factor is found, then to find the other dimension just divide N by that factor.
• 1 divides all whole numbers. Don't leave it out, in case N is prime.
• If len is the length and wid is the width, then the perimeter is found using 2*len + 2*wid
Problem
Your program should repeatedly read a positive integer, the number of pictures to be laid out.
For each input, it should print the smallest possible perimeter for a filled rectangle that is
formed by laying all the pictures edge-to-edge. Also print the dimensions of this rectangle.
This suggests a nested loop: A conditional outer loop is needed to detect either the number of
photographs or C == 0 (end of the program), and a counted inner loop (a for loop) is needed to
find the minimum perimeter for an arrangement of photos.
You will also add a third, conditional loop to detect for bad input. While this goes at the top of
the inner loop, add it last after you debug the rest of your code. Assume that the user is
generally literate and can follow instructions they read on your prompts ("Please input the
number of photographs: ", for example). What you are looking for are negative numbers. Other
forms of bad data like strings will cause Java to throw an exception.
Sample Session
Please input your number of photographs: 100
Minimum perimeter is 40 with dimension 10 x 10.
Please input your number of photographs: 15
Minimum perimeter is 16 with dimension 3 x 5.
Please input your number of photographs: 195
Minimum perimeter is 56 with dimension 13 x 15.
Please input your number of photographs: 0
Goodbye!