Untitled
unknown
plain_text
3 years ago
1.1 kB
5
Indexable
""" Exercise 4 """ def main(): #Input from the user to set the limit number. limit = int(input("Please enter the limit number: ")) counter = 0 if (limit < 5): print("\nNo existing triplet with this limit.") else: # The lowest triplet is 3, 4, 5 - we started each operator from the lowest triplet. for c in range (5, limit+1): # 'b' cannot surpass 'c', by stopping at 'c' we actually stop at c-1 - which means 'b' will never be greater than 'c'. for b in range (4, c): # b is set to be bigger than a to avoid printing a triplet twice - for example 3 4 5 and 4 3 5. for a in range (3, b): #The formula for the Pythagorean theorem. if (a**2 + b**2 == c**2): print ("a =", a ,"| b =", b ,"| c =", c) #Every time the loop repeats itself, counter will add 1 to the amount of triplets. counter += 1 print("\nThe amount of Pythagorean triplets you have is:", counter) main()
Editor is loading...