062.scm 899 B

123456789101112131415161718192021222324252627282930
  1. ;; Cubic permutations
  2. (use-modules (euler utils))
  3. (define (find-n-cubic-permutation n)
  4. (define cubic-perms '())
  5. (define (search-loop i)
  6. (let* ((cube (expt i 3))
  7. (key (number->key cube)))
  8. (add-perm key cube)
  9. (if (>= (get-perm-count key) n)
  10. (get-min-perm-val key)
  11. (search-loop (1+ i)))))
  12. (define (add-perm key val)
  13. (set! cubic-perms
  14. (assoc-set! cubic-perms
  15. key
  16. (list (set-min-perm-val key val) (1+ (get-perm-count key))))))
  17. (define (get-perm-count key)
  18. (let ((entry? (assoc-ref cubic-perms key)))
  19. (if entry? (cadr entry?) 0)))
  20. (define (get-min-perm-val key)
  21. (car (assoc-ref cubic-perms key)))
  22. (define (set-min-perm-val key val)
  23. (let ((entry? (assoc-ref cubic-perms key)))
  24. (if entry? (car entry?) val)))
  25. (search-loop 1))
  26. (define (number->key n)
  27. (string-concatenate (map number->string (sort (number->digits n) <))))