p052.scm 868 B

123456789101112131415161718192021222324252627282930
  1. ;; Permuted multiples
  2. (define-module (euler solved p052))
  3. (use-modules (srfi srfi-1)
  4. (srfi srfi-26)
  5. (wak foof-loop)
  6. (euler utils))
  7. (define* (solve #:optional (multipliers (iota 5 2)))
  8. (min-permuted-multiple multipliers))
  9. ;;; TODO: better name/move logic to solve
  10. (define (min-permuted-multiple multipliers)
  11. (loop continue ((for multiplicand (up-from 1)))
  12. (if (all-products-permutations? multiplicand multipliers)
  13. multiplicand
  14. (continue))))
  15. (define (all-products-permutations? multiplicand multipliers)
  16. (fold-and (cut permuted-multiple? multiplicand <>)
  17. multipliers))
  18. (define (permuted-multiple? num multiplier)
  19. (integer-permutation? num (* num multiplier)))
  20. (define (integer-permutation? n1 n2)
  21. (and (= (number-length n1) (number-length n2))
  22. (digits=? n1 n2)))