# Repetition in the output

Hi all,
I have tried Pythagorean triples program coding …but the output is repeated twice and thrice etc…also (2,0,2) and (4,3,5) should not be included in my triples. please point the location where I have done the mistake.

N=int(input('enter the upper limit: '))

p=int(input('enter a no : '))

while (p<=N):

for n in range(1,p):

``````if (p<=2) & (p >=(q and r)):

print("INVALID")

else:

if p%2!=0:

q=int(((p*p)/2)-0.5)

r=int(((p*p)/2)+0.5)

#triple=(p,q,r)

print((p,q,r));

elif(p%2==0):

q=int(((p/2)**2)-1)

r=int(((p/2)**2)+1)

#triple=(p,q,r)

print((p,q,r));

else:

print("greater than the natural no N")
``````

p=p+1

Hi and welcome.

I’ve done some reformatting on your code and re-posted here. I trust that I’ve not misinterpreted anything.

``````N=int(input('enter the upper limit: '))

p=int(input('enter a no : '))

while (p <= N):
for n in range(1,p):
if (p <=2 ) & (p >= (q and r)):
print("INVALID")
else:
if p%2 != 0:
q = int(((p*p)/2)-0.5)
r = int(((p*p)/2)+0.5)
#triple=(p,q,r)
print((p,q,r))

elif(p%2 == 0):
q=int(((p/2)**2)-1)
r=int(((p/2)**2)+1)
#triple=(p,q,r)
print((p,q,r))
else:
print("greater than the natural no N")
p = p+1
``````

The issue with running your code as is, is that both `q` and `r` have not been defined before the `while` loop kicks in and as such the logic is not easy to follow.

Also, I’m unsure what the `p = p+1` is for at the end.

edit: hold up, maybe it’s part of the final `else` statement.
Done.

Hi Rob,

`` Thanks a lot...whatever u said the same the same I have worked here. what a coincidence! But still I have one thing that my 'p' should not be greater than 'q' and also 'q' not greater than 'r'. I have changed p<=2 & p>q>r...it still gives output (4,3,5)..Is there any other way to avoid (4,3,5)?``

This is reframed code now.

N=int(input('enter the upper limit: '))

p=int(input('enter a no : '))

while (p<=N):

for n in range(1,p):

``````q=4

r=5

if(p<=2)|(p>q>r):

print(" INVALID NUMBER ")

elif p%2!=0:

q=int(((p*p)/2)-0.5)

r=int(((p*p)/2)+0.5)

#triple=(p,q,r)

print((p,q,r));

elif(p%2==0):

q=int(((p/2)**2)-1)

r=int(((p/2)**2)+1)

#triple=(p,q,r)

print((p,q,r));

else:

print("greater than the natural no N")

p=p+1``````

This satisfies the logic that you have there:

``````r = 5
q = 4
p = 3

if p <= q:
print('no, p is not > q')
else:
print('yes, p is > q')

if q <= r:
print('no, q is not > r')
else:
print('yes, q is > r')
``````

… not too sure if that’s of any help.

b.t.w: these posts use ‘backtics’ as a way of tagging code (it’s called markdown) so it’s better not to use them of anything else because in doing so, you make it real hard to read your posts.

too many if else makes complicate to reread my pgm…I need short and sweet Rob. btw am able to get ur ‘btw’ .u mean I markeddown my code and sent if am not wrong?

am not able to get*

That’s better

Should your `while` loop not be:

``````while (p<=N):
for N in range(p):
q=4
r=5
``````

?? (upper case `N`)

edit: or rather the `for` loop within said

No…here N in range(p) throws only ‘p’ is no of times as output…like p=3 only 3 triples but N is my upper limit…ie.no of triples

Oh well, sorry: just a thought.

Math is not my strong suit: I can work the math if I define the problem, but I find it not so easy to do when someone else defines the problem.

Oh…but u have helped a lot Rob…thanks in tons…

Thank you and you’re very welcome: helping others (or tying to least ways) is in my nature.

am novice here…u have big heart…It gives confidence that someone is here to lift me up…u did…have a great day.

No worries.

For the most part, you’ll find that this is a real helpful and friendly community.

Peace.

Thank you Rob

Rob’s code has two `if` and two `else`, making four conditions in total.

Your code had one `while`, `if`, two `elif` and one `else`, making five
conditions in total.

Rob’s code is simpler than yours

Vijayalakshmi said: “This is reframed code now.”

I have run your code and I see no repetition in the output.

I used:

``````enter the upper limit: 20
enter a no : 5
``````

and the output was 28 lines:

``````(5, 12, 13)
(6, 8, 10)
(7, 24, 25)
...
(30, 224, 226)
(31, 480, 481)
(32, 255, 257)
``````

with no repetition.

Let me clean up your code to make it written better for Python style.

``````
N = int(input('enter the upper limit: '))
p = int(input('enter a no : '))
while p <= N:
for n in range(1, p):
q = 4
r = 5
if (p <= 2) or (p > q > r):
print(" INVALID NUMBER ")

if p % 2 != 0:
q = int((p*p)/2 - 0.5)
r = int((p*p)/2 + 0.5)
else:
q = int((p/2)**2 - 1)
r = int((p/2)**2 + 1)

assert p**2	+ q**2 == r**2
# Pythagorean triple = (p, q, r)
print((p,q,r))
p = p + 1

``````

Hope that helps!

Hi Steven,

There is no doubt in Rob’s code compared to my programming way.But am customising the way I like to see my coding part.Thank you Steven, added value to someone helping in the community. More helping hands are here to my knowledge…
But still (2,02),(4,3,5) (please enter 2 instead of 5 in place of “p”)appear in conditions which will not satisfy the pythagorean rules and also what happened to my p>q>r part…Is it really working or not? If you feel I am not clear please let me know in a better manner.

Hi Rob,
Apologizes for speaking like that(I don’t have an intention to ignore your suggestions)