]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/006/006.factor
factor: Move math.ranges => ranges.
[factor.git] / extra / project-euler / 006 / 006.factor
1 ! Copyright (c) 2007, 2008 Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math ranges sequences project-euler.common ;
4 IN: project-euler.006
5
6 ! http://projecteuler.net/index.php?section=problems&id=6
7
8 ! DESCRIPTION
9 ! -----------
10
11 ! The sum of the squares of the first ten natural numbers is,
12 !     1² + 2² + ... + 10² = 385
13
14 ! The square of the sum of the first ten natural numbers is,
15 !    (1 + 2 + ... + 10)² = 55² = 3025
16
17 ! Hence the difference between the sum of the squares of the first ten natural
18 ! numbers and the square of the sum is 3025 - 385 = 2640.
19
20 ! Find the difference between the sum of the squares of the first one hundred
21 ! natural numbers and the square of the sum.
22
23
24 ! SOLUTION
25 ! --------
26
27 <PRIVATE
28
29 : sum-of-squares ( seq -- n )
30     0 [ sq + ] reduce ;
31
32 : square-of-sum ( seq -- n )
33     sum sq ;
34
35 PRIVATE>
36
37 : euler006 ( -- answer )
38     100 [1..b] [ sum-of-squares ] [ square-of-sum ] bi - abs ;
39
40 ! [ euler006 ] 100 ave-time
41 ! 0 ms ave run time - 0.24 SD (100 trials)
42
43 SOLUTION: euler006