+ 16

[ASSIGNMENT] Pythagorean Triplet with given sum

Pythagorean Triplet with given sum A Pythagorean Triplet is a set of natural numbers such that a < b < c, for which a^2 + b^2 = c^2. For example, 3^2 + 4^2 = 5^2. Given a number n, find a Pythagorean Triplet with sum as given n. Examples: Input : n = 12 Output : 3, 4, 5 Note that 3, 4 and 5 is a Pythagorean Triplet with sum equal to 12. Input : n = 4. Output : No Triplet There does not exist a Pythagorean Triplet with sum equal to 4. all language welcome already submitted as assignment

10th Apr 2018, 4:22 AM
Sudarshan Rai
Sudarshan Rai - avatar
18 Answers
10th Apr 2018, 6:55 AM
LukArToDo
LukArToDo - avatar
+ 10
https://code.sololearn.com/c0dW5Bl2Np5J/?ref=app look my..
10th Apr 2018, 4:47 PM
💞ⓢⓦⓐⓣⓘ💞
💞ⓢⓦⓐⓣⓘ💞 - avatar
+ 4
https://code.sololearn.com/ccfMZl2cZC9M/?ref=app
10th Apr 2018, 8:43 PM
Russ
Russ - avatar
+ 3
Well, this is what can be done quickly - an optimized brut-force solution: https://code.sololearn.com/cbsxwm8xVFOM/?ref=app I will think more about a better one... UPD: Ok, It can be done much better than in my previous attempt. My previous solution had complexity of O(n^2), whereas a new one has a complexity of O(n): https://code.sololearn.com/cwKYv1GofIri/?ref=app
10th Apr 2018, 5:14 AM
Nevfy
+ 3
@VcC And here is mine oneliner: https://code.sololearn.com/cj0EayVp2IoF/?ref=app
10th Apr 2018, 7:35 AM
Nevfy
+ 3
Other option is to test if x%1==0 ( this is x's non integer part)
10th Apr 2018, 8:31 AM
VcC
VcC - avatar
+ 2
@Louis That is how it will work: print('{0} Pythagorian Triplet'.format(*pt if len(pt)>0 else ['No'])) Your solution has a complexity of O(n^3), however it looks quite pretty like that. :-)
10th Apr 2018, 5:59 AM
Nevfy
+ 2
3^2 + 4^2 = 5^2 = 25 Yeah, thats right, thats why it is pythagorean triplet, but what I guess he meant is that a + b + c = n, as in his example 3 + 4 + 5 = 12
10th Apr 2018, 7:26 AM
Lucas Pardo
Lucas Pardo - avatar
+ 2
efficient oneliner. I did not get the problem.right - it is looking for b and c such that a b c is a square triangle https://code.sololearn.com/cg1WBB6x8aqG/?ref=app
10th Apr 2018, 7:33 AM
VcC
VcC - avatar
+ 2
You can also do : type(f) == int
10th Apr 2018, 7:41 AM
Lucas Pardo
Lucas Pardo - avatar
+ 2
@VcC Yes, but I afraid of precision of calculation, because: float(4.00001).is_integer() == False
10th Apr 2018, 7:45 AM
Nevfy
+ 1
n=25 in your example. not 12
10th Apr 2018, 6:56 AM
VcC
VcC - avatar
+ 1
Nevfy did you know f.is_integer() ?
10th Apr 2018, 7:37 AM
VcC
VcC - avatar
+ 1
No Lucas. Type(16**.5) will be float. is_integer(16**.5) will be true
10th Apr 2018, 8:07 AM
VcC
VcC - avatar
+ 1
Oh, I see what you mean, yeah, for that what I do is f == int(f), a little "dirty" but it works xD Good to know about .is_integer()
10th Apr 2018, 8:14 AM
Lucas Pardo
Lucas Pardo - avatar
8th Feb 2019, 3:36 AM
Vishwas Mishra
Vishwas Mishra - avatar