sqrt_2_approximation.sf 565 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # 01 December 2016
  4. # Approximation for sqrt(2).
  5. # Numerator
  6. # a(n) = cos(2^(n-1) * arccos(3))
  7. # Denominator
  8. # b(1) = 1
  9. # b(2) = 2
  10. # b(n) = 2 * a(n-2) * b(n-1)
  11. # Fun facts:
  12. # gcd(a(n), b(n+1)) = 1
  13. # cos(acos(3) / 2) = sqrt(2)
  14. # See also:
  15. # a(n) = https://oeis.org/A001601
  16. # b(n) = https://oeis.org/A051009
  17. # Numerator
  18. func a(n) {
  19. cos(2**(n-1) * acos(3))
  20. }
  21. # Denominator
  22. func b((1)) { 1 }
  23. func b((2)) { 2 }
  24. func b(n) is cached {
  25. 2 * b(n-1) * a(n-2)
  26. }
  27. for n in (1..10) {
  28. say a(n)/b(n+1)
  29. }