Exposing the body of perfect? as aliquot.

This commit is contained in:
Alex Shinn 2014-06-10 20:07:49 +09:00
parent 3ff62dc355
commit 1ab7d12b21
3 changed files with 14 additions and 7 deletions

View file

@ -205,15 +205,19 @@
((= 1 (gcd n i)) (lp (+ i 1) (+ count 1)))
(else (lp (+ i 1) count))))))
;;> The aliquot sum s(n), equal to the sum of proper divisors of an
;;> integer n.
(define (aliquot n)
(let ((limit (+ 1 (quotient n 2))))
(let lp ((i 2) (sum 1))
(cond ((> i limit) sum)
((zero? (remainder n i)) (lp (+ i 1) (+ sum i)))
(else (lp (+ i 1) sum))))))
;;> Returns true iff \var{n} is a perfect number, i.e. the sum of its
;;> divisors other than itself equals itself.
(define (perfect? n)
(and (> n 1)
(let ((limit (+ 1 (quotient n 2))))
(let lp ((i 2) (sum 1))
(cond ((> i limit) (= n sum))
((zero? (remainder n i)) (lp (+ i 1) (+ sum i)))
(else (lp (+ i 1) sum)))))))
(and (> n 1) (= n (aliquot n))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Random prime generation

View file

@ -1,7 +1,8 @@
(define-library (chibi math prime)
(import (scheme base) (scheme inexact) (srfi 27) (srfi 33))
(export prime? nth-prime prime-above prime-below factor perfect? totient
(export prime? nth-prime prime-above prime-below factor perfect?
totient aliquot
provable-prime? probable-prime?
random-prime random-prime-distinct-from
coprime? random-coprime modular-inverse modular-expt

View file

@ -70,6 +70,8 @@
(test '(2 2 3) (factor 12))
(test '(3 3 3 5 7) (factor 945))
(test 975 (aliquot 945))
(do ((i 3 (+ i 2)))
((>= i 101))
(test (number->string i) (prime? i)