1 ! Copyright (c) 2008 Aaron Schaefer.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel math ranges namespaces project-euler.common
4 sequences sequences.extras ;
7 ! https://projecteuler.net/problem=39
12 ! If p is the perimeter of a right angle triangle with integral
13 ! length sides, {a,b,c}, there are exactly three solutions for p
16 ! {20,48,52}, {24,45,51}, {30,40,50}
18 ! For which value of p < 1000, is the number of solutions
25 ! Algorithm adapted from
26 ! https://mathworld.wolfram.com/PythagoreanTriple.html
27 ! Identical implementation as problem #75
29 ! Basically, this makes an array of 1000 zeros, recursively
30 ! creates primitive triples using the three transforms and then
31 ! increments the array at index [a+b+c] by one for each triple's
32 ! sum AND its multiples under 1000 (to account for non-primitive
33 ! triples). The answer is just the index that has the highest
43 : adjust-p-count ( n -- )
44 max-p 1 - over <range> p-count get
45 [ [ 1 + ] change-nth ] curry each ;
47 : (count-perimeters) ( seq -- )
49 dup sum adjust-p-count
50 [ u-transform ] [ a-transform ] [ d-transform ] tri
51 [ (count-perimeters) ] tri@
56 : count-perimeters ( n -- )
57 0 <array> p-count set { 3 4 5 } (count-perimeters) ;
61 : euler039 ( -- answer )
63 1000 count-perimeters p-count get arg-max
66 ! [ euler039 ] 100 ave-time
67 ! 1 ms ave run time - 0.37 SD (100 trials)