1 ! Copyright (c) 2008 Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel math math.ranges
4 namespaces project-euler.common sequences ;
7 ! http://projecteuler.net/index.php?section=problems&id=75
12 ! It turns out that 12 cm is the smallest length of wire can be bent to form a
13 ! right angle triangle in exactly one way, but there are many more examples.
22 ! In contrast, some lengths of wire, like 20 cm, cannot be bent to form a right
23 ! angle triangle, and other lengths allow more than one solution to be found;
24 ! for example, using 120 cm it is possible to form exactly three different
25 ! right angle triangles.
27 ! 120 cm: (30,40,50), (20,48,52), (24,45,51)
29 ! Given that L is the length of the wire, for how many values of L ≤ 2,000,000
30 ! can exactly one right angle triangle be formed?
36 ! Algorithm adapted from http://mathworld.wolfram.com/PythagoreanTriple.html
37 ! Identical implementation as problem #39
39 ! Basically, this makes an array of 2000000 zeros, recursively creates
40 ! primitive triples using the three transforms and then increments the array at
41 ! index [a+b+c] by one for each triple's sum AND its multiples under 2000000
42 ! (to account for non-primitive triples). The answer is just the total number
43 ! of indexes that are equal to one.
52 : adjust-p-count ( n -- )
53 max-p 1- over <range> p-count get
54 [ [ 1+ ] change-nth ] curry each ;
56 : (count-perimeters) ( seq -- )
58 dup sum adjust-p-count
59 [ u-transform ] [ a-transform ] [ d-transform ] tri
60 [ (count-perimeters) ] tri@
65 : count-perimeters ( n -- )
66 0 <array> p-count set { 3 4 5 } (count-perimeters) ;
70 : euler075 ( -- answer )
72 2000000 count-perimeters p-count get [ 1 = ] count
75 ! [ euler075 ] 10 ave-time
76 ! 3341 ms ave run timen - 157.77 SD (10 trials)