169 Exploring the number of different ways a number can be expressed as a sum of powers of 2.sf 364 B

123456789101112131415161718192021222324
  1. #!/usr/bin/ruby
  2. # Author: Trizen
  3. # Date: 20 August 2016
  4. # Translated to Sidef: 18 March 2022
  5. # https://github.com/trizen
  6. # https://projecteuler.net/problem=169
  7. # Runtime: 0.243s
  8. func f((0)) { 0 }
  9. func f((1)) { 1 }
  10. func f(n { .is_even }) is cached {
  11. __FUNC__(n/2)
  12. }
  13. func f(n) is cached {
  14. __FUNC__((n-1)/2) + __FUNC__((n-1)/2 + 1)
  15. }
  16. say f(10**25 + 1)