素因数分解をしてみた
素因数分解をしてみたのでメモ
(defun primep-1 (n i) (cond ((> i (sqrt n)) T) ((zerop (mod n i)) nil) (t (primep-1 n (1+ i))))) (defun primep (number) (primep-1 number 2)) (defun next-prime (n) (cond ((primep n) n) (t (next-prime (1+ n))))) (defun factoring-1 (n divisor) (cond ((zerop n) n) ((primep n) (format t " ~A ~%" n) n) ((zerop (mod n divisor)) (format t "~A| ~A ~%----------~%" divisor n) (list divisor (factoring-1 (floor (/ n divisor)) divisor))) (t (factoring-1 n (next-prime (1+ divisor)))))) (defun factoring (n) (factoring-1 n 2))
こんな感じで使います
$ ros run ? (factoring 360) 2| 360 ---------- 2| 180 ---------- 2| 90 ---------- 3| 45 ---------- 3| 15 ---------- 5 (2 (2 (2 (3 (3 5))))) ?
※ 標準出力をちょっとだけ整えました
moremagic
2018-10-27