p032.scm 927 B

1234567891011121314151617181920212223242526272829
  1. (define-module (euler solved p032))
  2. (use-modules (srfi srfi-1)
  3. (euler utils))
  4. (define (find-n-pandigital-products n)
  5. (let multiply-loop ((i 2) (j 1) (candidates '()))
  6. (let ((product (* i j)))
  7. (cond
  8. ((> (number-length i) (ceiling (/ n 2)))
  9. (delete-duplicates candidates))
  10. ((>= j i) (multiply-loop (1+ i) 1 candidates))
  11. ((> (multiply-length i j product) n) (multiply-loop (1+ i) 1 candidates))
  12. ((n-pandigital? (list i j product) n)
  13. (multiply-loop i (1+ j) (cons product candidates)))
  14. (else (multiply-loop i (1+ j) candidates))))))
  15. (define (multiply-length i j product)
  16. (string-length (string-concatenate (map number->string (list i j product)))))
  17. (define (n-pandigital? lst n)
  18. (lset= string=? (map number->string (iota 9 1))
  19. (map string
  20. (string->list (string-concatenate
  21. (if (number? (car lst))
  22. (map number->string lst)
  23. lst))))))