Singular integer right triangles
Problem 075: Singular integer right triangles
It turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples.
12 cm: (3,4,5)
24 cm: (6,8,10)
30 cm: (5,12,13)
36 cm: (9,12,15)
40 cm: (8,15,17)
48 cm: (12,16,20)
In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.
120 cm: (30,40,50), (20,48,52), (24,45,51)
Given that L is the length of the wire,
for how many values of L <= 1,500,000
can exactly one integer sided right angle triangle be formed?
Solution:
AAA A A
# ... #
. . . .
. . .
. . . .
# ... #
> "}"8* 20p "}}`"** 30p 30g>:0\:20g%\20v
v p060$_^#!\-1:p+3/g<
v p08+1g08<
>180p>80g::*4*\6*2++30g`#v_>80g1+:90p>::2**\80g2**+30g` |
@.g06< ^p09:+1g09<
v g13g12p16++p14:+*:g08*:g09p13:**2g09g08p12:-*:g08*:g09<
>`#v_ >1>:61g*30g`#v_:21g7*31g+5*41g+# *81p:61g*:20g%\20g/3+g:#v_v
>21g31g21p31p^ ^+1 <># $# ^# _v#<`\0_v#-g18:<
^p06-1g06p+3/g02\%g02:\-10*g16:<v1# <
^p06+1g06p+3/g02\%g02:\g18*g16:$<^ $<
Explanation:
Luckily there is a simple formula to generate Pythagorean triples.
a = k * (m*m - n*n);
b = k * (2*m*n);
c = k * (m*m + n*n);
We have a cache of the size 1500001
the we go through all the values for m
and n
where 2*m*m + 2*m*n <= LIMIT
.
When we have found a triple we look in the cache at position a+b+c
:
- If the value is not set (0) we write the the triple-hash (
(a*7 + b)*5 + c
) at the position and increment the result. - If there is already an hash that equals with our current triple we do nothing
- If there is already an different hash we write
-1
in the cache and decrement the result. - If there is an
-1
in the cache we do nothing
So at the end we have the amount of sum's with exactly one solution in our result-value.
We have to test for equal hashes because its possible to find the same tuple multiple times (with different k
values).
Interpreter steps: | 293 080 647 |
Execution time (BefunExec): | 39s (7.34 MHz) |
Program size: | 1000 x 1515 |
Solution: | 161667 |
Solved at: | 2015-08-26 |