Advertisement
andrejpodzimek

The Infinite Square Root (and Golden Ratio at q == 1)

May 7th, 2020
1,390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. #!/bin/env python3
  2.  
  3. # Infinite square root: x = √(q + √(q + √(q + …)))
  4. # Basic equation: 0 = x² − x − q
  5. # Higher order equation: 0 = xᵃ − bx − c
  6.  
  7. Q_MAX = 6  # highest q to calculate
  8. EXPONENT_MAX = 6  # highest log₂(a) to calculate
  9. LINEAR_MAX = 2 ** EXPONENT_MAX
  10.  
  11. def exponential_next(q):
  12.   return lambda a, b, c: (2 * a, b**2 + 2 * b * c, q * b**2 + c**2)
  13.  
  14. def linear_next(q):
  15.   return lambda a, b, c: (2 + a, b * (q + 1) + c, q * (b + c))
  16.  
  17. def single_next(q):
  18.   return lambda a, b, c: (1 + a, b + c, q * b)
  19.  
  20. def typeset(a, b, c):
  21.   n = ''.join('⁰¹²³⁴⁵⁶⁷⁸⁹'[ord(d) - ord('0')] for d in str(a))
  22.   return f'0 = x{n} − {b if b != 1 else ""}x − {c}'
  23.  
  24. def print_series(q, nsteps, step):
  25.   a = (2, 1, q)
  26.   next_a = step(q)
  27.   while a[0] < nsteps:
  28.     print(typeset(*a))
  29.     a = next_a(*a)
  30.   print(typeset(*a))
  31.  
  32. def print_quotient(q):
  33.   print(f'x = √({q} + √({q} + √({q} + …)))')
  34.   print(f'x² = {q} + √({q} + √({q} + …))')
  35.   print()
  36.   for note, step in (('Exponential:', exponential_next),
  37.                      ('Linear by 2:', linear_next),
  38.                      ('Linear by single step:', single_next)):
  39.     print(note)
  40.     print_series(q, LINEAR_MAX, step)
  41.  
  42. for q in range(1, Q_MAX):
  43.   print_quotient(q)
  44.   print()
  45. print_quotient(Q_MAX)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement