Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
31
Task/Amicable-pairs/Racket/amicable-pairs.rkt
Normal file
31
Task/Amicable-pairs/Racket/amicable-pairs.rkt
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#lang racket
|
||||
(require "proper-divisors.rkt")
|
||||
(define SCOPE 20000)
|
||||
|
||||
(define P
|
||||
(let ((P-v (vector)))
|
||||
(λ (n)
|
||||
(set! P-v (fold-divisors P-v n 0 +))
|
||||
(vector-ref P-v n))))
|
||||
|
||||
;; returns #f if not an amicable number, amicable pairing otherwise
|
||||
(define (amicable? n)
|
||||
(define m (P n))
|
||||
(define m-sod (P m))
|
||||
(and (= m-sod n)
|
||||
(< m n) ; each pair exactly once, also eliminates perfect numbers
|
||||
m))
|
||||
|
||||
(void (amicable? SCOPE)) ; prime the memoisation
|
||||
|
||||
(for* ((n (in-range 1 (add1 SCOPE)))
|
||||
(m (in-value (amicable? n)))
|
||||
#:when m)
|
||||
(printf #<<EOS
|
||||
amicable pair: ~a, ~a
|
||||
~a: divisors: ~a
|
||||
~a: divisors: ~a
|
||||
|
||||
|
||||
EOS
|
||||
n m n (proper-divisors n) m (proper-divisors m)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue