Picture Perfect
InterviewTime limit1sMemory limit128 MB
For each C, find the factor pair (W, H) with W*H = C closest to a square, and report the smallest perimeter along with W and H.
- Level
Easy3 of 10
- Topics
- Math, Brute force, Number theory, Implementation
- Solved
- No attempts yet
Problem
Roy has a stack of student yearbook photos. He wants to lay them on a flat surface edge to edge to form a completely filled rectangle whose perimeter is as small as possible. Every photo must be fully visible, and each photo is a square measuring unit.
For example, photos can be arranged as follows, where each photo is drawn as an X:
XXXX
XXXX
XXXX
They could also be turned the other way:
XXX
XXX
XXX
XXX
Both arrangements are completely filled rectangles with the same perimeter, units.
For a given number of photos , find the filled rectangle formed by placing all photos edge to edge that has the smallest possible perimeter, and report both that perimeter and the rectangle's dimensions.
Input
The input contains several test cases. Each test case is a single positive integer (), the number of photos to lay out. The input ends with a line containing , which must not be processed.
Output
For each value of , print one line in exactly this format:
Minimum perimeter is P with dimensions W x H
Here is the smallest possible perimeter, and and are the side lengths of the rectangle that achieves it, with .