+ 3

How to improve the code?

The object of the program is to identify all the Pythagorean triples for : a + b + c = N such that a^2 + b^2 = c^2 where a < b < c I tried to use the logic: a + b + c = N such that a^2 + b^2 = c^2 where a < b < c and a = 2mn, b = m^2 - n^2, c = m^2 + n^2 where m > n but it would not produce all 8 triplets for N = 840 which I had got from a Euler proof. What else could be used to speed this up? https://code.sololearn.com/cpuk3QOT0Gdb/?ref=app

23rd Jan 2023, 10:24 PM
William Owens
William Owens - avatar
4 odpowiedzi
+ 2
William Owens in about half of the iterations c is negative so it is doing unnecessary calculations.
24th Jan 2023, 5:46 PM
Brian
Brian - avatar
+ 1
If I run your code for input 840, it does show 8 results (correctly). Did you manage to fix it? or I don't see the bug.
24th Jan 2023, 5:18 AM
Tibor Santa
Tibor Santa - avatar
+ 1
I was testing with N=840, but you can cause c to go negative with only N=2.
25th Jan 2023, 2:20 AM
Brian
Brian - avatar
0
Brian What are you using for N to get the negatives?
25th Jan 2023, 12:59 AM
William Owens
William Owens - avatar