mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-21 14:49:18 +02:00
Exposing the body of perfect? as aliquot.
This commit is contained in:
parent
3ff62dc355
commit
1ab7d12b21
3 changed files with 14 additions and 7 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue