p027.scm 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. ;; Quadratic primes
  2. (ns (euler solved p027))
  3. (use (argyle)
  4. ((euler primes) :select (prime?))
  5. (euler utils)
  6. ((srfi srfi-1) :select (reduce zip)))
  7. (def p027-solve (b-max :o (c-max :or b-max))
  8. (->> (reduce >primes
  9. default-quad
  10. (gen-quads b-max c-max))
  11. prn-quad))
  12. (data quad (a b c primes)
  13. :init (quad a b c)
  14. :app (fn (n)
  15. (+ (* (quad-a self) (^ n 2))
  16. (* (quad-b self) n)
  17. (quad-c self))))
  18. (def default-quad (quad 0 0 0))
  19. (def >primes (a b)
  20. (= a (calc-primes a))
  21. (= b (calc-primes b))
  22. (if (> (quad-primes a) (quad-primes b))
  23. a b))
  24. (def gen-quads (b-max c-max)
  25. (collect-list (for b (up-from (- b-max) (to b-max)))
  26. (for c (up-from (- c-max) (to c-max)))
  27. (quad 1 b c)))
  28. (def calc-primes (quad)
  29. (if (quad-primes quad) quad
  30. (loop ((for n (up-from 0))
  31. (while (prime? (quad n))))
  32. => (quad-primes! quad n))))
  33. (def prn-quad (quad)
  34. (app format "n^2 + ~sn + ~s has ~s quadratic prime(s)"
  35. (map (\\ _ quad)
  36. `(,quad-b ,quad-c ,quad-primes))))