What happens
I 'm solving challenge 54 from CodeAbbey and I’m stuck because a logical failure.
What do you understand or find about that problem
Actually in my immersion process I’m using hy
, nonetheless, I wrote a python code trying to understand the logic behind finding pythagorean triples.
This video was really usefull to take in the problem: Pythagorean Triples Generator (Nevertheless, I think that this solution doesn’t encompass all possible universes of triples).
You make any workaround? What did you do?
In the evidences section I’m attaching the python code (just pure logic) that evaluates what triplet satisfy the condition: a + b + c = S
where S
will be the input from Codeabbey (I’m using just one value, 30 in this case —the triplet is 12 5 13).
I known that values from CodeAbbey will be greater than 10e+7
.
Why fails your workaround?
I spent almost all day long trying to generate a nested recursion pseudoloop (and I made it) but I don’t know why it keeps the recursion and doesn’t stop when satisfy realSum == entrySum
, also attaching a screenshot of the output.
Evidences
Evidence 1 (Code):
# Generate the triplet with m and n
def genTriplet (m, n):
a = 2 * m * n
b = (m * m) - (n * n)
c = (m * m) + (n * n)
summ = a + b + c
return a, b, c, summ
def serieGenerator (m, n, entrySum):
a,b,c,realSum = genTriplet(m,n)
if n >= m:
return
else:
if realSum == entrySum:
return print(m,n,"/","EL TRIANGULO CON SUMA DE LADOS", entrySum, "ES:",a,b,c)
else:
print(m,n,"/","sides=",a,b,c,"/","sum=",realSum)
serieGenerator(m, n+1, entrySum)
def pythaTriplet (limit, m, n, entrySum):
a,b,c,realSum = genTriplet(m,n)
if m > limit:
return
elif realSum == entrySum:
return
else:
serieGenerator(m, n, entrySum)
pythaTriplet(limit, m + 1, n, entrySum)
# Run the main function with a limit of 10
# 2 and 1 are the first test (because m > n always)
# And the sum of the triplet to find is 30
pythaTriplet(10, 2, 1, 30)
Evidence 2 (Output):
2 1 / sides= 4 3 5 / sum= 12
3 1 / sides= 6 8 10 / sum= 24
3 2 / EL TRIANGULO CON SUMA DE LADOS 30 ES: 12 5 13
4 1 / sides= 8 15 17 / sum= 40
4 2 / sides= 16 12 20 / sum= 48
4 3 / sides= 24 7 25 / sum= 56
5 1 / sides= 10 24 26 / sum= 60
5 2 / sides= 20 21 29 / sum= 70
5 3 / sides= 30 16 34 / sum= 80
5 4 / sides= 40 9 41 / sum= 90
...
...
10 1 / sides= 20 99 101 / sum= 220
10 2 / sides= 40 96 104 / sum= 240
10 3 / sides= 60 91 109 / sum= 260
10 4 / sides= 80 84 116 / sum= 280
10 5 / sides= 100 75 125 / sum= 300
10 6 / sides= 120 64 136 / sum= 320
10 7 / sides= 140 51 149 / sum= 340
10 8 / sides= 160 36 164 / sum= 360
10 9 / sides= 180 19 181 / sum= 380
I need help with
Is not totally evident why the recursion never stop, Why is that? I’m assuming that when realSum == entrySum
it should stop…but no, it doesn’t.
A logical, theoric or specific help would be great hand.
Thanks!