Repetition in the output

Looks as if you’ve sussed Markdown for your code blocks :+1:

The return statement would be return -1, A
Mind, I’m unsure what the -1 is for, but I assume you have your reasons for that.

I very much enjoyed reading and learning about assert. tyvm for that.

It’s not very good practice to use built-in function name(s) (eval() in this case) as names of functions you define.

Credits goes to u both only for my successful implementation. :innocent: Have u checked my 2nd code? give your suggestions to hide (2,0,2) (4,3,5) to get a desired output. I am also working on it.
-1 nothing but in return simply says failure.

Hi Aivar,
Do you mean to use evaluate() instead of eval() for better understanding?

Steven, wheels on his legs…I think.
I will read more about assert…thanks for adding valuable gem to my code…its glowing now.

I thought it would be something like that.

So, what inputs are you using that produce an output of (2,0,2) and/or (4,3,5) ? I can’t seem to replicate that output.

Technically, (2, 0, 2) does satisfy the rules for a Pythagorean Triple:

2**2 + 0**2 == 2**2

In mathematics we call that a degenerate case.

The triangle (4, 3, 5) is the same triangle as (3, 4, 5), the legs are just flipped.

Try this:

def triples(upper_limit, start):
    p = start
    while p <= upper_limit:
        psqr = p**2
        if p % 2 == 1:
            q = int(psqr/2 - 0.5)
            r = int(psqr/2 + 0.5)
            q = int(psqr/4 - 1)
            r = int(psqr/4 + 1)
        # Pythagorean triple = (p, q, r)
        assert p**2 + q**2 == r**2
        if q > p:
        p += 1
1 Like

In my pythagorean code

It is working exactly :innocent:

But how the condition satisfied here?..let me go step by step to understand this code. thank you Steven. U made my day.

N=int(input('enter the upper limit: '))
p=int(input('enter a no : '))
while (p<=N):
  if p%2!=0: 
  assert p**2 + q**2 == r**2      
  if q > p :

This is my final code…

I am over the moon. Thank you soooo much…whetted my appetite…

Now you’ve been bitten buy the coding bug, you’ll find that there are not enough hrs in the day!!

On a final note: one can use x += 1 or x -= y or whatever instead of x = x + 1 etc.

Haha…thanks for reminding me x+=1 also…well begun is half done…u have started well yesterday…today ended well… :stuck_out_tongue_closed_eyes:

1 Like

Thanks a lot to u both…

I am going to sleep peacefully…

Hi Rob and Steven…Hope you are doing good…I will be back soon with my doubts…I have a lot more to learn from u all…