Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Assignment 2: School Yearbook

Roy has a stack of student yearbook photos.


He wants to lay the pictures on a flat surface
edge-to-edge to form a filled rectangle with
minimum perimeter. All photos must be fully
visible. Each picture is a square with
dimensions 1 unit by 1 unit.

For example, he would place 12 photos in


the following configuration, where each
photo is indicated with an X.

XXXX
XXXX
XXXX

Of course, he could orient them in the other


direction, such as
XXX
XXX
XXX
XXX

which would have the same perimeter, 14 units.

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.

An input value of C = 0 will terminate the program.

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!

You might also like