Problem 23
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.
C#
~203 ms on Intel T7500
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Numerics; using System.IO; namespace Euler { class Program { static bool isAbundant(int n) { int sum = 0; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { sum += i; int q = n / i; if (q != n && q != i) sum += q; if (sum > n) return true; } } return false; } static void Main(string[] args) { var startTime = DateTime.Now; const int max = 28123; var abundantNums = new HashSet<int>(Enumerable.Range(2, max).Where(isAbundant)); int sum = 0; for (int i = 1; i <= max; i++) { foreach (int a in abundantNums) { if (a >= i) break; int b = i - a; if (abundantNums.Contains(b)) { goto Next; } } //Console.WriteLine(i); sum += i; Next: ; } Console.WriteLine(sum); Console.WriteLine((DateTime.Now - startTime).TotalMilliseconds + " ms"); Console.ReadKey(); } } }
Racket
(define (abundant? n) (define sum 0) (let loop ([i 1]) (if (<= (* i i) n) (if (zero? (modulo n i)) (begin (set! sum (+ sum i)) (let ([q (quotient n i)]) (when (and (not (= q n)) (not (= q i))) (set! sum (+ sum q))) (if (> sum n) #t (loop (add1 i))))) (loop (add1 i))) #f))) (define max 28123) (define abundantNums (filter abundant? (range 2 (add1 max)))) (define abundantNumsSet (list->set abundantNums)) (define sum 0) (let loop ([i 1]) (when (<= i max) ; (when (not (ormap (lambda (x) (set-member? abundantNumsSet (- i x) )) abundantNums)) (unless (for/or ([x abundantNumsSet]) (set-member? abundantNumsSet (- i x))) (set! sum (+ sum i))) (loop (add1 i)))) (displayln sum) ; ; ; using let/ec (faster) (define (abundant? n) (define sum 0) (let loop ([i 1]) (if (<= (* i i) n) (if (zero? (modulo n i)) (begin (set! sum (+ sum i)) (let ([q (quotient n i)]) (when (and (not (= q n)) (not (= q i))) (set! sum (+ sum q))) (if (> sum n) #t (loop (add1 i))))) (loop (add1 i))) #f))) (define max 28123) (define abundantNums (list->set (filter abundant? (range 2 (add1 max))))) (define sum 0) (let loop ([i 1]) (when (<= i max) (set! sum (+ sum (let/ec break (for ([x abundantNums]) (when (>= x i) (break i)) (when (set-member? abundantNums (- i x)) (break 0)))i))) (loop (+ 1 i)))) (displayln sum)
'Project Euler' 카테고리의 다른 글
Project Euler Problem 24 (0) | 2012.06.08 |
---|---|
Project Euler Solutions in Clojure: clojure-euler (0) | 2012.06.05 |
Project Euler Problem 22 (0) | 2012.06.03 |
Project Euler Problem 21 (0) | 2012.06.03 |
Project Euler Problem 20 (0) | 2012.06.03 |