p049.scm 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. ;; Prime permutations
  2. (define-module (solved p049))
  3. (use-modules (euler utils)
  4. (euler primes))
  5. (define (n-digit-increasing-prime-sequence-t n)
  6. (define primes-bit (erato-bit (expt 10 n)))
  7. (define primes (filter (n-digit-number? n) (prime-bitvector->lst primes-bit)))
  8. (let lp ([curr-diff 1] [primes primes] [acc '()])
  9. (cond
  10. [(null? primes) acc]
  11. [(> (+ (car primes) (* 2 curr-diff)) (expt 10 n))
  12. (lp 1 (cdr primes) acc)]
  13. [else
  14. (let seq-lp ([val (+ curr-diff (car primes))]
  15. [curr-acc (list (car primes))])
  16. (cond
  17. [(> val (expt 10 n))
  18. (lp (1+ curr-diff) primes
  19. (if (>= (length curr-acc) 3)
  20. (cons curr-acc acc)
  21. acc))]
  22. [(and (bitvector-ref primes-bit val)
  23. (permutation? val (car curr-acc)))
  24. (seq-lp (+ val curr-diff) (cons val curr-acc))]
  25. [else (lp (1+ curr-diff) primes acc)]))])))
  26. (define (n-digit-increasing-prime-sequence n)
  27. (let* ([primes-bit (erato-bit (expt 10 n))]
  28. [primes (filter (n-digit-number? n) (prime-bitvector->lst primes-bit))])
  29. (let lp ([curr-diff 1] [primes primes])
  30. (if (> (+ (car primes) (* 2 curr-diff)) (expt 10 n))
  31. (lp 1 (cdr primes))
  32. (let seq-lp ([val (car primes)] [acc '()])
  33. (cond
  34. [(> val (expt 10 n)) acc]
  35. [(and (bitvector-ref primes-bit val)
  36. (if (null? acc) #t (permutation? val (car acc))))
  37. (seq-lp (+ val curr-diff) (cons val acc))]
  38. [else (lp (1+ curr-diff) primes)]))))))
  39. (define (n-digit-number? n)
  40. (lambda (num)
  41. (= n (length (number->digits num)))))