077 Prime summations.sf 584 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/ruby
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Website: https://github.com/trizen
  5. # What is the first value which can be written as the sum of primes in over five thousand different ways?
  6. # https://projecteuler.net/problem=77
  7. # Runtime: 1.755s
  8. var primes = []
  9. func count(n, i=0, sum=0) is cached {
  10. return 1 if (sum == n)
  11. return 0 if (sum > n)
  12. return 0 if (i > primes.end)
  13. count(n, i+1, sum) +
  14. count(n, i, sum + primes[i])
  15. }
  16. for i in (4 .. 1e6) {
  17. primes = (i-2).primes
  18. if (count(i) > 5000) {
  19. say i
  20. break
  21. }
  22. }