Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

* Only using WHILE loops* *no FOR loops!* I need help writing a Pythagorean Trip

ID: 3669218 • Letter: #

Question

* Only using WHILE loops* *no FOR loops!* I need help writing a Pythagorean Triples python 3 algorithm. Can't use *any* FOR loops! Only use while loops. Also, dont use floating point arithmetic. Has to be just like the instructions listed below.

* Only using WHILE loops* *no FOR loops!* (Sorry for repeating, this is my 3rd time asking this question and getting it answered the wrong way!)

Instructions:

Write a Python program that will output all of the pythogorean triples in a specified interval. A pythogorean triple, (a,b,c) consists of three integers a < b < c such that the sum of the squares of the two smaller integers is equal to the square of the largest. Your program will input an integer value n > 10 from the user (keyboard input) and, using a series of nested while loops, determine and print out all the pythagorean triples with hypotenuse values ( length c ) in the closed interval [1, n] (one triple per line).

You also need to add a validate/retry loop to insure that the user enters an acceptable upper bound value. Allow the user 3 tries before giving up.

Explanation / Answer

#see Euclid's pythogorean triple generating formula perimeter = [0]*1001 triples = [] for m in range(1,24): for n in range(m+1,24): c = m*m + n*n a = abs(m*m - n*n) b = 2*m*n p = a+b+c w = [a,b,c] w.sort() # w = [p] + w k = 1 # if not(w in triples): triples += [w] # counter = 0 while p < 1001: # print a,b,c,p w = [i*k for i in w] if w==[2*8,2*15,2*17] and k == 2: print w if not(w in triples): triples += [w] # if w==[3*16,3*30,3*34] and k == 3: print triples # if w == [8,15,17]: print 'yes',k,p # w = [a,b,c] # w.sort() # w = [p] + w k += 1 p *= k if w==[2*8,2*15,2*17] and k == 3: print p # if w == [8,15,17]: print 'yes',k,p,p