/extra/project-euler/169/169.factor

http://github.com/abeaumont/factor · Factor · 42 lines · 11 code · 12 blank · 19 comment · 1 complexity · 7848b6f4369b70c339f3bf01bf142116 MD5 · raw file

  1. ! Copyright (c) 2007 Samuel Tardieu.
  2. ! See http://factorcode.org/license.txt for BSD license.
  3. IN: project-euler.169
  4. USING: combinators kernel math math.functions memoize project-euler.common ;
  5. ! http://projecteuler.net/index.php?section=problems&id=169
  6. ! DESCRIPTION
  7. ! -----------
  8. ! Define f(0) = 1 and f(n) to be the number of different ways n can be
  9. ! expressed as a sum of integer powers of 2 using each power no more than
  10. ! twice.
  11. ! For example, f(10) = 5 since there are five different ways to express 10:
  12. ! 1 + 1 + 8
  13. ! 1 + 1 + 4 + 4
  14. ! 1 + 1 + 2 + 2 + 4
  15. ! 2 + 4 + 4
  16. ! 2 + 8
  17. ! What is f(10^25)?
  18. ! SOLUTION
  19. ! --------
  20. MEMO: fn ( n -- x )
  21. {
  22. { [ dup 2 < ] [ drop 1 ] }
  23. { [ dup odd? ] [ 2/ fn ] }
  24. [ 2/ [ fn ] [ 1 - fn ] bi + ]
  25. } cond ;
  26. : euler169 ( -- result )
  27. 10 25 ^ fn ;
  28. ! [ euler169 ] 100 ave-time
  29. ! 0 ms ave run time - 0.2 SD (100 trials)
  30. SOLUTION: euler169