0
The question is to print all the Pythagorean triplet from a accepted range.I am getting repetitive values ex:3,4 and 4,3.
import math a=int(input("start range")) b=int(input("end range")) for c in range(a,b+1): for d in range(a,b+1): for l in range(a,b+1): p=c**2+d**2 if l==math.sqrt(p): print (c,d,l) this is the code I wrote. I am getting the answer but I get repetative values ex3,4 ,5and 4,3,5. I only need 3,4,5 or 4,3,5 .how to I print without repetition. please just correct my mistake I don't want a new logic to solve this problem. I am relatively new to Python
4 Answers
+ 1
thanks a lot for the answers
0
Hi shivaani!
As per your request, here's the smallest amount of change that'd fix your code: Modify line 5 to
for d in range(c,b+1):
This ensures that the second number in the Pythagorean triplet is no less than the first, thus removing duplicates.
Hope that helps đ
0
Jay Matthews, the OP didn't want a different algorithm.
0
here's it, ask if you have any questionđ
https://code.sololearn.com/caTIqKpqdXSR