p058.scm 768 B

12345678910111213141516171819202122
  1. ;; Spiral primes
  2. ;; Generate erato sieve bitvector
  3. ;; We need a function that can go through this vector and determine what is on the diagonal.
  4. ;; Recursive function to find diagonals:
  5. ;; i = 1 , val = 1 -> i = 2 -> val = 4
  6. (define-module (solved p058))
  7. (use-modules (euler primes))
  8. ;; Using miller primality test since we need to check large primes
  9. (define (side-length-of-spiral-when-n-percent-of-diagonals-are-prime n)
  10. (let lp ([side 4] [i 9] [pass 3] [tests 5])
  11. (if (< (/ pass tests) n) (begin (display i) (newline) (1- side))
  12. (lp (+ 2 side) (+ i(* side 4))
  13. (let side-lp ([i (+ i side)] [diagonals 3] [pass pass])
  14. (if (zero? diagonals) pass
  15. (side-lp (+ i side) (1- diagonals)
  16. (if (prime? i) (1+ pass) pass))))
  17. (+ 4 tests)))))