Metallic ratios: Difference between revisions

+python
(+python)
Line 1,012:
Golden ratio, where b = 1:
Value to 256 dp after 615 iterations: 1.61803398874989484...2695486262963136144 (258 digits)
</pre>
 
=={{header|Python}}==
<lang python>from itertools import count, islice
from _pydecimal import getcontext, Decimal
 
def metallic_ratio(b):
m, n = 1, 1
while True:
yield m, n
m, n = m*b + n, m
 
def stable(b, prec):
def to_decimal(b):
for m,n in metallic_ratio(b):
yield Decimal(m)/Decimal(n)
 
getcontext().prec = prec
last = 0
for i,x in zip(count(), to_decimal(b)):
if x == last:
print(f'after {i} iterations:\n\t{x}')
break
last = x
 
for b in range(4):
coefs = [n for _,n in islice(metallic_ratio(b), 15)]
print(f'\nb = {b}: {coefs}')
stable(b, 32)
 
print(f'\nb = 1 with 256 digits:')
stable(1, 256)</lang>
{{out}}
<pre>b = 0: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
after 1 iterations:
1
 
b = 1: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
after 77 iterations:
1.6180339887498948482045868343656
 
b = 2: [1, 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363, 8119, 19601, 47321, 114243]
after 43 iterations:
2.4142135623730950488016887242097
 
b = 3: [1, 1, 4, 13, 43, 142, 469, 1549, 5116, 16897, 55807, 184318, 608761, 2010601, 6640564]
after 33 iterations:
3.3027756377319946465596106337352
 
b = 1 with 256 digits:
after 613 iterations:
1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137484754088075386891752126633862223536931793180060766726354433389086595939582905638322661319928290267880675208766892501711696207032221043216269548626296313614
</pre>
 
Anonymous user