Challenge
Origami (folding paper) is a creative form of art. As far as I know, master of Origami prefers square paper. Let's start from beginning - convert a rectangular paper to a square one.
So the paper is divided into squares. We remove the biggest square which shares one shorter edge with the current shape, step by step (see the picture below). And if the remaining part after one step is less or equal than 0.001 * (area of the original paper)
, the paper cannot be divided any further. It is possible that nothing remains at last.
Your task is to calculate how many squares are made during the process. The square in the last step that makes the paper unable to be divided is counted into the output.
Example (a paper of 1.350
width/height), output is 10:
Input and Output
Input: width / height ratio for the rectangular paper, one decimal (or an integer without the dot) from 1.002
to 1.999
with a minimal step of 0.001
. You may also use any other reasonable format describing the ratio. Just mention it in your answer.
Output: square count, one integer.
Example I/O
A mapping format is used to keep the page tidy, while your code doesn't need to support a list input nor to be a mapping function.
1.002 => 251
1.003 => 223
1.004 => 189
1.005 => 161
1.006 => 140
1.007 => 124
1.008 => 111
1.009 => 100
Thanks to @LuisMendo, here is the graph of answers.
Remarks
- This is a code-golf so shortest code wins
- Pay attention to standard loopholes
- It's your freedom to decide how to deal with input and output but they should follow standard restrictions.
By the way...
- Comment if you have anything unclear about the challenge
- Personally I would suggest your answer contains a explanation if you are using a golfing language
- Thanks to @GregMartin, read his answer for a good mathematical explanation for the challenge.
Example Code
Here is a ungolfed version of C++ code:
#include <iostream>
#include <utility>
int f (double m)
{
double n = 1, k = 0.001;
int cnt = 0;
k *= m; // the target minimum size
while(m*n >= k)
{
m -= n; // extract a square
if(n > m)
std::swap(n, m); // keep m > n
++ cnt;
}
return cnt;
}
int main()
{
double p;
std::cin >> p;
std::cout << f(p);
return 0;
}
All calculations related in the example code need an accuracy of 6 decimal digits, which is covered in float
.