rob42
(Rob)
May 14, 2022, 6:04pm
44
Looks as if you’ve sussed Markdown for your code blocks
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.
@steven.daprano
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.
vijivijay
(Vijayalakshmi)
May 14, 2022, 6:31pm
46
Credits goes to u both only for my successful implementation. Have u checked my 2nd code?..pl 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.
vijivijay
(Vijayalakshmi)
May 14, 2022, 6:33pm
47
Hi Aivar,
Do you mean to use evaluate() instead of eval() for better understanding?
vijivijay
(Vijayalakshmi)
May 14, 2022, 6:36pm
48
Steven, wheels on his legs…I think.
I will read more about assert…thanks for adding valuable gem to my code…its glowing now.
rob42
(Rob)
May 14, 2022, 6:42pm
49
Vijayalakshmi:
Credits goes to u both only for my successful implementation. Have u checked my 2nd code?..pl 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.
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)
else:
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:
print((p,q,r))
p += 1
vijivijay
(Vijayalakshmi)
May 14, 2022, 6:53pm
53
But how the condition satisfied here?..let me go step by step to understand this code. thank you Steven. U made my day.
vijivijay
(Vijayalakshmi)
May 14, 2022, 7:15pm
54
N=int(input('enter the upper limit: '))
p=int(input('enter a no : '))
while (p<=N):
q=4
r=5
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
if q > p :
print(p,q,r)
p=p+1
This is my final code…
vijivijay
(Vijayalakshmi)
May 14, 2022, 7:20pm
55
I am over the moon. Thank you soooo much…whetted my appetite…
rob42
(Rob)
May 14, 2022, 7:24pm
56
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.
vijivijay
(Vijayalakshmi)
May 14, 2022, 7:29pm
57
Haha…thanks for reminding me x+=1 also…well begun is half done…u have started well yesterday…today ended well…
vijivijay
(Vijayalakshmi)
May 14, 2022, 7:45pm
59
I am going to sleep peacefully…
vijivijay
(Vijayalakshmi)
May 16, 2022, 4:40pm
60
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…