+ 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
18 Answers
+ 10
https://code.sololearn.com/c0dW5Bl2Np5J/?ref=app
look my..
+ 4
https://code.sololearn.com/ccfMZl2cZC9M/?ref=app
+ 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
+ 3
@VcC And here is mine oneliner:
https://code.sololearn.com/cj0EayVp2IoF/?ref=app
+ 3
Other option is to test if x%1==0 ( this is x's non integer part)
+ 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. :-)
+ 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
+ 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
+ 2
You can also do : type(f) == int
+ 2
@VcC Yes, but I afraid of precision of calculation, because: float(4.00001).is_integer() == False
+ 1
n=25 in your example. not 12
+ 1
Nevfy did you know f.is_integer() ?
+ 1
No Lucas. Type(16**.5) will be float. is_integer(16**.5) will be true
+ 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()