100 Arranged probability -- v2.sf 450 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 17 February 2017
  4. # License: GPLv3
  5. # https://github.com/trizen
  6. # https://projecteuler.net/problem=100
  7. # Runtime: 0.188s
  8. func a(n) is cached {
  9. n == 0 && return 0
  10. n == 1 && return 3
  11. 6*a(n-1) - a(n-2) + 2
  12. }
  13. func b(n) is cached {
  14. n == 0 && return 1
  15. n == 1 && return 3
  16. 6*b(n-1) - b(n-2) - 2
  17. }
  18. for n in (1 .. Inf) {
  19. if (a(n) > 1e12) {
  20. say b(n)
  21. break
  22. }
  23. }