mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-21 22:59:16 +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)))
|
((= 1 (gcd n i)) (lp (+ i 1) (+ count 1)))
|
||||||
(else (lp (+ i 1) count))))))
|
(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
|
;;> Returns true iff \var{n} is a perfect number, i.e. the sum of its
|
||||||
;;> divisors other than itself equals itself.
|
;;> divisors other than itself equals itself.
|
||||||
(define (perfect? n)
|
(define (perfect? n)
|
||||||
(and (> n 1)
|
(and (> n 1) (= n (aliquot n))))
|
||||||
(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)))))))
|
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; Random prime generation
|
;; Random prime generation
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
|
|
||||||
(define-library (chibi math prime)
|
(define-library (chibi math prime)
|
||||||
(import (scheme base) (scheme inexact) (srfi 27) (srfi 33))
|
(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?
|
provable-prime? probable-prime?
|
||||||
random-prime random-prime-distinct-from
|
random-prime random-prime-distinct-from
|
||||||
coprime? random-coprime modular-inverse modular-expt
|
coprime? random-coprime modular-inverse modular-expt
|
||||||
|
|
|
@ -70,6 +70,8 @@
|
||||||
(test '(2 2 3) (factor 12))
|
(test '(2 2 3) (factor 12))
|
||||||
(test '(3 3 3 5 7) (factor 945))
|
(test '(3 3 3 5 7) (factor 945))
|
||||||
|
|
||||||
|
(test 975 (aliquot 945))
|
||||||
|
|
||||||
(do ((i 3 (+ i 2)))
|
(do ((i 3 (+ i 2)))
|
||||||
((>= i 101))
|
((>= i 101))
|
||||||
(test (number->string i) (prime? i)
|
(test (number->string i) (prime? i)
|
||||||
|
|
Loading…
Add table
Reference in a new issue