]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/006/006.factor
project-euler: Rewrap, update links, add copyrights, tests
[factor.git] / extra / project-euler / 006 / 006.factor
1 ! Copyright (c) 2007, 2008 Aaron Schaefer.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: kernel math ranges sequences project-euler.common ;
4 IN: project-euler.006
5
6 ! https://projecteuler.net/problem=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
18 ! first ten natural numbers and the square of the sum is 3025 -
19 ! 385 = 2640.
20
21 ! Find the difference between the sum of the squares of the
22 ! first one hundred natural numbers and the square of the sum.
23
24
25 ! SOLUTION
26 ! --------
27
28 <PRIVATE
29
30 : sum-of-squares ( seq -- n )
31     0 [ sq + ] reduce ;
32
33 : square-of-sum ( seq -- n )
34     sum sq ;
35
36 PRIVATE>
37
38 : euler006 ( -- answer )
39     100 [1..b] [ sum-of-squares ] [ square-of-sum ] bi - abs ;
40
41 ! [ euler006 ] 100 ave-time
42 ! 0 ms ave run time - 0.24 SD (100 trials)
43
44 SOLUTION: euler006