+ 7

Express any number as a sum of as few squares as possible

All in the title. Ex: 16=4**2 15=1**2+1**2+3**2+2**2 191=9*9+3*3+10*10+1*1 Very interesting answer from Louis so he wins even if his code is not solving 100% of the problem.

3rd Feb 2018, 1:54 PM
VcC
VcC - avatar
19 Answers
4th Feb 2018, 1:49 PM
Louis
Louis - avatar
+ 8
Got it also 191 = 13²+3²+3²+2² https://code.sololearn.com/cLBdlCIrckam/?ref=app
4th Feb 2018, 5:58 AM
Louis
Louis - avatar
3rd Feb 2018, 2:41 PM
Louis
Louis - avatar
+ 5
@VcC Thanks, yes I agree, it may not catch all combinations of 4. I will think about it, maybe use all items in 2dictionary and add them together 1 by one to create a 4dictionary. So show us your mindboggling oneliner.
4th Feb 2018, 3:43 PM
Louis
Louis - avatar
+ 4
Here you are my friend. Complexity could be reduced by testing if n is a square or if n is in q but would make the oneliner longer ;-) https://code.sololearn.com/cYArjkqNaSzO/?ref=app
4th Feb 2018, 5:07 PM
VcC
VcC - avatar
+ 2
@Louis Your code is fast and gives one decomposition but not the smaller one. ex for 191 you give 5 numbers but there are shorter options...
3rd Feb 2018, 3:22 PM
VcC
VcC - avatar
+ 2
Here is another way of doing It with combinations, but I don't know if It will be quick enough for big numbers here in sololearn, but I tried It on the pc and It works perfectly, still thinking how to fix my other code btw xD https://code.sololearn.com/cpXR0OVS4X4C/?ref=app
5th Feb 2018, 2:03 PM
Lucas Pardo
Lucas Pardo - avatar
+ 1
very interesting code. very good idea to use sets. the only limitation is that you are not sure to end up with a sum of 4.
4th Feb 2018, 3:09 PM
VcC
VcC - avatar
+ 1
@louis i m finishing it because your idea made improve it
4th Feb 2018, 4:48 PM
VcC
VcC - avatar
+ 1
It's a little too late, but I did something that seems to work, finding maximum 4 factors for each number, see if you like It xD https://code.sololearn.com/coTo6XHEMyzE/?ref=app
5th Feb 2018, 12:56 PM
Lucas Pardo
Lucas Pardo - avatar
+ 1
not working for 704
5th Feb 2018, 1:01 PM
VcC
VcC - avatar
0
Yep was a mistake. It was 10 not 2. You only need 4 squares
3rd Feb 2018, 10:16 PM
VcC
VcC - avatar
0
Now go and code !
3rd Feb 2018, 10:25 PM
VcC
VcC - avatar
0
Not working for 704=8**2+8**2+24**2 or for 384=8,8,16
4th Feb 2018, 7:11 AM
VcC
VcC - avatar
0
Yeah, there are some that can be done with less than 4 numbers and my Code does It with 4, thats the problem right?
5th Feb 2018, 1:03 PM
Lucas Pardo
Lucas Pardo - avatar
0
@lucas you only need to consider x>=y>=z>=t tuples. look at louis' or my code for additionnal imprvements
5th Feb 2018, 4:19 PM
VcC
VcC - avatar