
(defun recurring-cycle (d) "Calculate the length of the recurring cycle for 1/d." (labels ((remainders (d r rs) (if (eql r 0) 0 (let ((s (mod r d))) (if (member s rs) (1+ (position s rs)) (remainders d (* 10 s) (cons s rs))))))) (remainders d 1 nil))) (defun p26 () "Find the number below 1000 with the longest recurring cycle in its decimal representation." (let* ((results (loop :for n :from 1 :to 999 collect (cons n (recurring-cycle n)))) (max-pair (reduce #'(lambda (a b) (if (> (cdr a) (cdr b)) a b)) results))) (car max-pair)))
; SLIME 2.28 CL-USER> (p26) 983 CL-USER>
— Me@2025-01-20 03:57:23 PM
.
.
2025.01.20 Monday (c) All rights reserved by ACHK

You must be logged in to post a comment.