Project Euler with Befunge
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?
AAA A A
# ... #
. . . .
. . .
. . . .
# ... #
> "}"8* 20p "}}`"** 30p 30g>:0\:20g%\20v
>`#v_ >1>:61g*30g`#v_:21g7*31g+5*41g+# *81p:61g*:20g%\20g/3+g:#v_v
>21g31g21p31p^ ^+1 <># $# ^# _v#<`\0_v#-g18:<
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
2*m*m + 2*m*n <= LIMIT.
When we have found a triple we look in the cache at position
- 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
-1in the cache and decrement the result.
- If there is an
-1in 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
|Interpreter steps:||293 080 647|
|Execution time (BefunExec):||39s (7.34 MHz)|
|Program size:||1000 x 1515|