p037.scm 883 B

1234567891011121314151617181920212223242526272829303132
  1. ;; Truncable primes
  2. (ns (euler solved p037))
  3. (use (argyle)
  4. ((euler primes) #:select (prime?)))
  5. ;;; find sum of only 11 truncable primes
  6. (def solve ()
  7. (app + (truncatable-primes)))
  8. (def truncatable-primes ()
  9. (loop ((for n (up-from 10))
  10. (where primes '()
  11. (if (and (prime? n) (truncatable? n))
  12. (cons n primes)
  13. primes))
  14. (while (< (len primes) 11)))
  15. => primes))
  16. ;;; Expects n to be prime
  17. (def truncatable? (n)
  18. (and-map prime? `(,@(truncables left-truncate n)
  19. ,@(truncables right-truncate n))))
  20. (def truncables (truncator n)
  21. (if (< n 10) '()
  22. (let n (truncator n)
  23. (cons n (truncables truncator n)))))
  24. (def left-truncate (n) (num (str-drop (str n) 1)))
  25. (def right-truncate (n) (num (str-take (str n)
  26. (1- (len (str n))))))