Python-Tutorial-2
Gold
What is the gold number ?
I want to appology because I didn't understood everything about
it during the lesson, here is what I understood:
if you take a piece of paper for a standard printer, if you
fold it in two along the longer side, then the ratio between
the greater side and the smaller side will remain the same.
And if you do it again, and again, it will always be the
same.
So here is how we can check this with the
python3 command:
$ python3
Python 3.11.2
>>> 29.7 / 21
1.4142857142857141
We can divide the greater side, as if we fold the piece of
paper in two:
>>> 29.7 / 2
14.85
Then if we divide again 21 by this result
we see that we get the same ratio:
>>> 21 / 14.85
1.4141414141414141
So the math is:
(y / x) == (x / (y / 2))
As you can see, this is the same number that you can
get with the square-root of 2:
>>> import math
>>> math.sqrt(2)
1.4142135623730951
I want to appology, because I will not be able to explain
why this is like this, I didn't understood the lesson at
school.
>>> 29.7 / 21
1.4142857142857141
Only the first decimals are the same because the real number
is not 29.7, it should be
29.7 with other decimals after
29.7.....
We can try to write a function which will make the result
converge to this real number:
$ python3
Python 3.11.2
>>> def is_gold(x, y):
... r1 = y / x
... r2 = x / (y / 2)
... rd = abs(r1 - r2)
... return rd
...
>>> is_gold(21, 29.7)
0.00014430014430000249
>>> is_gold(21, 29.71)
0.0010964722475999
>>> is_gold(21, 29.72)
0.0020483240402484615
The abs() function returns the absolute value.
An absolute value of a negative value, is its positive
equivalent.
So the real number for 29.7 is probably:
$ python3
Python 3.11.2
>>> import math
>>> math.sqrt(2) * 21
29.698484809834998
We can see that when I was searching one decimal after
29.7 it was just going more far from the
result.
This is because I was not searching in the right interval.
In order to search a result in the right interval, we should
probably search from 21, because for this ratio
we search a value which is greater than 21.
Indeed a ratio between two values is supposed to be a number
between 0 and 1, this is why we
have to divide the smaller value by the greater one.
Now that we have our is_gold() function, and
that we have been able to figure out in which interval we
have to search, we can try to write a function to search
a more accurate value for 29.7.
$ python3
Python 3.11.2
>>> def is_gold(x, y):
... r1 = y / x
... r2 = x / (y / 2)
... rd = abs(r1 - r2)
... return rd
...
>>> is_gold(21, 21)
1.0
>>> rd = 1
>>> is_gold(21, 21.1)
0.9857594222523132
>>> rd2 = 0.9857594222523132
>>> step = 0.1
>>> is_gold(21, 21.2)
0.9716082659478886
>>> res = (1, 21)
>>> x = 21
>>> y = 21
>>> while y < 30:
... rd2 = is_gold(x, y)
... if rd2 < rd:
... res = (rd2, y)
... y += step
...
>>> res
(0.019127249562044257, 29.900000000000126)
Here we search in the interval
(21, 30) with a
step of
0.1, so we probably get a result with
an accuracy of 0.1:
29.9
If we search with a smaller step, we can probably find
a more accurate result.
$ python3
Python 3.11.2
>>> def is_gold(x, y):
... r1 = y / x
... r2 = x / (y / 2)
... rd = abs(r1 - r2)
... return rd
...
>>> x = 21
>>> y = 21
>>> res = (1, 21)
>>> step = 0.1
>>> while y < 30:
... rd2 = is_gold(x, y)
... print(f"rd2, y = {rd2}, {y}")
... y += step
...
....
rd2, y = 0.02857142857141714, 29.40000000126
rd2, y = 0.01896690879740581, 29.50000000127
rd2, y = 0.00939510939509769, 29.60000000122
rd2, y = 0.00014430014431192, 29.70000000124
rd2, y = 0.00965164589326851, 29.80000000125
rd2, y = 0.01912724956204427, 29.90000000126
We can see that indeed the most accurate result
29.7, is with the smaller difference:
0.00014430014431192.
A more accurate result could then be search'd with
a smaller
step of
0.01,
0.001, or
0.0001.
The output will then be very long, so we can try to
reduce the interval arround the previous result:
(29.6, 29.7)
Going Further
You can try to ask to some one who has an accademic knowledge.
Usage Notice
© 2025 Florent Monnier
License for the tutorial parts:
license: FDL
License for the code parts:
To the extent permitted by law:
spdx=any