I have this mathematical, Python problem, where I have to find all numbers in a range which are the sum of maximum 4 square numbers. I can't think about a working algorithm, with my basic knowledge. Can you help me out with the algorithm, or an idea of where to start? I'm not asking for the code. Thanks in advance!
Asked
Active
Viewed 142 times
-2
-
You should head to https://math.stackexchange.com/ – Foxan Ng Nov 16 '17 at 13:53
-
3What do you mean by "maximum square numbers"? A famous theorem in number theory states that *any* positive integer equals the sum of at most four squares of positive integers. So according to the usual meaning of your words, the entire range satisfies your criterion (at least the nonnegative part of it). – Rory Daulton Nov 16 '17 at 13:53
-
1Maybe he needs to identify these numbers? – Reblochon Masque Nov 16 '17 at 13:56
-
This is the solution, I needed to find all numbers in this range which can be defined as a sum of max. 4 perfect squares. So the output is the whole range, and that was the good solution. Thanks for everyone, have a nice day! – siposbalitn0 Nov 16 '17 at 14:10
-
See also [this answer](https://stackoverflow.com/a/41556521/5459839) on how to generate those four squares. – trincot Nov 16 '17 at 16:34
2 Answers
2
According to this Lagrange theorem, you can return the whole positive range because
every natural number can be represented as the sum of four integer squares.
It means that the algorithm can be written as:
def my_algorithm(integer_range):
return [i for i in integer_range if i >= 0]

Eric Duminil
- 52,989
- 9
- 71
- 124
0
Check this link out.
This wikipedia page will solve your problem!
Basically, all whole numbers can be represented as a sum of 4 perfect squares.

Tushar Aggarwal
- 827
- 1
- 10
- 26