Problem 23¶
Non-Abundant Sums¶
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of $28$ would be $1 + 2 + 4 + 7 + 14 = 28$, which means that $28$ is a perfect number.
A number $n$ is called deficient if the sum of its proper divisors is less than $n$ and it is called abundant if this sum exceeds $n$.
As $12$ is the smallest abundant number, $1 + 2 + 3 + 4 + 6 = 16$, the smallest number that can be written as the sum of two abundant numbers is $24$. By mathematical analysis, it can be shown that all integers greater than $28123$ can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
非過剰数和¶
完全数とは, その数の真の約数の和がそれ自身と一致する数のことである. たとえば, 28の真の約数の和は, 1 + 2 + 4 + 7 + 14 = 28 であるので, 28は完全数である.
真の約数の和がその数よりも少ないものを不足数といい, 真の約数の和がその数よりも大きいものを過剰数と呼ぶ.
12は, 1 + 2 + 3 + 4 + 6 = 16 となるので, 最小の過剰数である. よって2つの過剰数の和で書ける最少の数は24である. 数学的な解析により, 28123より大きい任意の整数は2つの過剰数の和で書けることが知られている. 2つの過剰数の和で表せない最大の数がこの上限よりも小さいことは分かっているが, 数学的な解析ではこの上限を減らすことは出来ていない.
2つの過剰数の和で書き表せない正の整数の総和を求めよ.
(import (scheme base)
(gauche base)
(math prime)
(scheme vector)
(scheme list))
(define (non-primes num)
(let ([primes-num (take-while (^n (<= n num)) (primes))]
[iota-4-num (iota (+ num 1 -4) 4)])
(lset-difference = iota-4-num primes-num)))
(define (proper-divisors num)
(let loop ([n 2] [result '()])
(cond
[(< num (* n n)) (delete-duplicates (cons 1 result))]
[(zero? (mod num n))
(loop (+ n 1) (cons* n (div num n) result))]
[else (loop (+ n 1) result)])))
(define (sum-divisors lis)
(map
(^n (cons n
(apply + (proper-divisors n))))
lis))
(define (abundant-nums num)
(let ([divisors-list (sum-divisors (non-primes num))])
(map (^c (car c))
(filter (^c (< (car c) (cdr c)))
divisors-list))))
(define (abundant-combination num)
(let* ([temp-num (abundant-nums num)])
(filter (^[cell] (<= (+ (car cell) (cdr cell)) num))
(fold (^[a a-lis]
(append (fold (^[b b-lis] (cons (cons a b) b-lis))
'()
temp-num)
a-lis))
'()
temp-num))))
(define (abundant-combination-sums num)
(let ([temp (map (^c (+ (car c) (cdr c)))
(abundant-combination num))]
[table (make-vector (+ num 1) #f)])
(for-each (^n (set! (vector-ref table n) n))
temp)
(vector-fold (^[lis num-or-f]
(if (boolean num-or-f)
(cons num-or-f lis)
lis))
'()
table)))
(define non-abundant-sums
(lset-difference =
(iota 28123 1)
(abundant-combination-sums 28123)))
(define answer-23
(apply + non-abundant-sums))
(format #t "23: ~d~%" answer-23)
Noppi が2024/01/05に更新 · 2件の履歴