Sum of two squares | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

Sum of two squares

How get sum of two squares if we have number N? Example: input 20 output 20 = 4^2 + 2^2

16th Jan 2017, 4:48 PM
Sergey
6 Answers
+ 10
Python. but you can write only algorithm I can write code self.
16th Jan 2017, 6:34 PM
Sergey
+ 10
Thank you!
16th Jan 2017, 6:38 PM
Sergey
+ 10
Thank you Carlos Ospina, but your code is wrong. I have code which is my Playground (look "sum of two squares" https://code.sololearn.com/cmQFXrR0fIiX/?ref=app ), but I need more quickly algorithm...
17th Jan 2017, 1:51 PM
Sergey
+ 6
""" Solution in Python. Check my codes to prove it """ r = N = int(input("N: ")) sqr = [] for x in range(2, N+1)[N//2::-1]: if x**2 <= r: sqr.append("{}^2".format(x)) r -= x**2 sqr += ["1^2"]*r print ("{} = {}".format(N, " + ".join(sqr)))
17th Jan 2017, 1:52 AM
Carlos A. Ospina
Carlos A. Ospina - avatar
+ 3
in what language?
16th Jan 2017, 6:28 PM
ifl
ifl - avatar
17th Jan 2017, 1:17 AM
Carlos A. Ospina
Carlos A. Ospina - avatar