PageRenderTime 81ms CodeModel.GetById 75ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/extra/project-euler/021/021.factor

http://github.com/abeaumont/factor
Unknown | 38 lines | 26 code | 12 blank | 0 comment | 0 complexity | e4d9f35264a6ca68752bce9cdf3c7a7f MD5 | raw file
 1! Copyright (c) 2007 Aaron Schaefer.
 2! See http://factorcode.org/license.txt for BSD license.
 3USING: combinators.short-circuit kernel math math.functions
 4    math.ranges namespaces project-euler.common sequences ;
 5IN: project-euler.021
 6
 7! http://projecteuler.net/index.php?section=problems&id=21
 8
 9! DESCRIPTION
10! -----------
11
12! Let d(n) be defined as the sum of proper divisors of n (numbers less than n
13! which divide evenly into n).
14
15! If d(a) = b and d(b) = a, where a != b, then a and b are an amicable pair and
16! each of a and b are called amicable numbers.
17
18! For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44,
19! 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4,
20! 71 and 142; so d(284) = 220.
21
22! Evaluate the sum of all the amicable numbers under 10000.
23
24
25! SOLUTION
26! --------
27
28: amicable? ( n -- ? )
29    dup sum-proper-divisors
30    { [ = not ] [ sum-proper-divisors = ] } 2&& ;
31
32: euler021 ( -- answer )
33    10000 [1,b] [ dup amicable? [ drop 0 ] unless ] map-sum ;
34
35! [ euler021 ] 100 ave-time
36! 335 ms ave run time - 18.63 SD (100 trials)
37
38SOLUTION: euler021