057 Square root convergents.sf 440 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/ruby
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Website: https://github.com/trizen
  5. # https://projecteuler.net/problem=57
  6. # Runtime: 0.310s
  7. func kn((0)) { 1 }
  8. func kn((1)) { 0 }
  9. func kn( n ) is cached {
  10. 2*kn(n-1) + kn(n-2)
  11. }
  12. func kd((0)) { 0 }
  13. func kd((1)) { 1 }
  14. func kd( n ) is cached {
  15. 2*kd(n-1) + kd(n-2)
  16. }
  17. say (1..1000 -> count_by {|k|
  18. var x = kn(k)
  19. var y = kd(k)
  20. (x+y).ilog10 > y.ilog10
  21. })