I came across this theorem one time and found it especially striking. I even told my friends who supposedly dislike math about it, and they were amazed, trying to calculate values for various numbers.

Lagrange's Four-Square Theorem states that any positive integer can be expressed as the sum of four perfect squares. Try this for numbers like 7, 31, 326. While we know of course of numbers that can be expressed as one square (perfect squares), we know less about numbers that can be expressed as the sum of two or three squares. (Note that 0 is a square). Try to work out this theorem for some other numbers? Can you find any that are the sum of only two squares or only three squares? Is there any pattern to these numbers?

While the proof of the theorem is beyond the scope of the Cosines Group, you can find the proof on the Wikipedia page with the same name.

As a Computer Science extension, can you create a code that will find the four squares that sum up to any given inputted number? Perhaps you can use this to locate any patterns in the system.

Feel free to post any solutions, ideas, questions, extensions, or code below.

## Comments

Sort by:

TopNewestYes I can! – Agnishom Chattopadhyay

Log in to reply

This can be done in mathematica with this code: PowersRepresentations[n,4,2] Here is an interesting algorithm on it: http://www.alpertron.com.ar/4SQUARES.HTM

Source: http://www.mathisfunforum.com/viewtopic.php?id=19225 – Agnishom Chattopadhyay

Log in to reply

class Four_squares { public static void main(int n) { for(int a=0;a<=n;a++) for(int b=0;b<=n;b++) for(int c=0;c<=n;c++) for(int d=0;d<=n;d++) if(a

a+bb+cc+dd==n) System.out.println(""+a+b+c+d); } } – Ayush AlankarLog in to reply

– Bob Krueger

Short and simple. I like it. However, it's not efficient and its output is the cleanest. Are there any easy fixes to this?Log in to reply

– Agnishom Chattopadhyay

Very Ineffecient, sorry!Log in to reply

superb – Debjyoti Chattopadhyay

Log in to reply

– Bob Krueger

Thank you!Log in to reply

Sorry, started making a post and then realized that it was out of depth for #CosinesGroup, so I moved it. – Calvin Lin Staff

Log in to reply

This is so awesome. – Elliott Macneil

Log in to reply

Also, try the PowerRepresentations command in Mathematica – Agnishom Chattopadhyay

Log in to reply

can you please tell what 'mod' means ? PLEASE !!!!!!!!!!!!!!!!!!!!!! I THINK YOU R GENIUS !! – Harsh Shrivastava

Log in to reply

Modular Arithmetic – Alexander Borisov

Considering numbers "mod 4" stands for looking at their remainders when divided by \(4.\) Note that if you know the remainders, when divided by \(4\), of any two numbers, you can calculate the remainder of their sum and product. For example, if \( a\) has reminder \(2\) and \(b\) has remainder \(3,\) their sum will always have remainder \(1.\) You may want to take a look here:Log in to reply

– Harsh Shrivastava

THANKSLog in to reply

By the way, the numbers that can be expressed as a sum of

twosquares have beencompletely characterized.Specifically, a number \(n\) is a sum of two squares if and only if each prime divisor that is \(3\) mod \(4\) has an even exponent in the prime factorization of \(n.\) – Michael TangLog in to reply

Well it is nice to prove this, here are some guidelines:

Prove that the product of two numbers that can both be represented as sums of two squares, can be represented as a sum of two squares.

Look at \(\pmod 4\), obviously.

Another obvious fact but is used, the product of two squares is a square, and can be represented as a sum of two squares obviously.

You may use Fermat's Christmas Theorem (as it was in a letter dated on 1640's Christmas)..

This should be enough. – Yong See Foo

Log in to reply

– Bob Krueger

Interesting!Log in to reply

You can also check Goldbach's theorem which states that every positive even number can be expressed as the sum of two prime numbers – Prashanth Kumar

Log in to reply

Goldbach Conjecture Wikipedia page has a pretty accurate (though long) description. – Alexander Borisov

This is actually not a theorem, but a conjecture, one of the most famous in Number Theory. There are some very interesting partial results, obtained using highly sophisticated methods.TheLog in to reply