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

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # Date: 20 August 2016
  4. # License: GPLv3
  5. # Website: https://github.com/trizen
  6. # https://projecteuler.net/problem=169
  7. # Runtime: 0.091s
  8. use 5.010;
  9. use strict;
  10. use warnings;
  11. use Memoize qw(memoize);
  12. use Math::AnyNum qw(:overload);
  13. memoize('f');
  14. sub f {
  15. my ($n) = @_;
  16. return 0 if $n == 0;
  17. return 1 if $n == 1;
  18. return f($n / 2) if $n % 2 == 0;
  19. f(($n - 1) / 2) + f(($n - 1) / 2 + 1);
  20. }
  21. say f(10**25 + 1);