/extra/project-euler/148/148.factor

http://github.com/abeaumont/factor · Factor · 54 lines · 15 code · 17 blank · 22 comment · 0 complexity · f31ca188e6325544bd1a120ca5ce1a43 MD5 · raw file

  1. ! Copyright (c) 2008 Eric Mertens.
  2. ! See http://factorcode.org/license.txt for BSD license.
  3. USING: kernel math math.functions sequences project-euler.common ;
  4. IN: project-euler.148
  5. ! http://projecteuler.net/index.php?section=problems&id=148
  6. ! DESCRIPTION
  7. ! -----------
  8. ! We can easily verify that none of the entries in the first seven rows of
  9. ! Pascal's triangle are divisible by 7:
  10. ! 1
  11. ! 1 1
  12. ! 1 2 1
  13. ! 1 3 3 1
  14. ! 1 4 6 4 1
  15. ! 1 5 10 10 5 1
  16. ! 1 6 15 20 15 6 1
  17. ! However, if we check the first one hundred rows, we will find that only 2361
  18. ! of the 5050 entries are not divisible by 7.
  19. ! Find the number of entries which are not divisible by 7 in the first one
  20. ! billion (10^9) rows of Pascal's triangle.
  21. ! SOLUTION
  22. ! --------
  23. <PRIVATE
  24. : sum-1toN ( n -- sum )
  25. dup 1 + * 2/ ; inline
  26. : >base7 ( x -- y )
  27. [ dup 0 > ] [ 7 /mod ] produce nip ;
  28. : (use-digit) ( prev x index -- next )
  29. [ [ 1 + * ] [ sum-1toN 7 sum-1toN ] bi ] dip ^ * + ;
  30. : (euler148) ( x -- y )
  31. >base7 0 [ (use-digit) ] reduce-index ;
  32. PRIVATE>
  33. : euler148 ( -- answer )
  34. 10 9 ^ (euler148) ;
  35. ! [ euler148 ] 100 ave-time
  36. ! 0 ms ave run time - 0.17 SD (100 trials)
  37. SOLUTION: euler148