問題
解答
ほとんど書き下しただけです。注意点は完全数を計算に含めないことくらいでしょうか。
(use srfi-1)
(define (divisors n)
(define (divisor? m) (zero? (remainder n m)))
(let loop ((i 2) (early '()) (later '()))
(if (> (* i i) n) (cons 1 (append-reverse early later))
(cond
((= (* i i) n) (loop (+ i 1) (cons i early) later))
((divisor? i) (loop (+ i 1) (cons i early) (cons (/ n i) later)))
(else (loop (+ i 1) early later))))))
(define (d n) (apply + (divisors n)))
(define (amicable-number? n)
(define m (d n))
(and (not (= n m)) (= n (d m))))
(define (solve)
(apply + (filter amicable-number? (iota (- 10000 1) 1))))
(define (main argv)
(display (solve))
(newline))
コメント
コメントを投稿