Given a number N, the task is to find the number of ways of writing N as a sum of 4 squares. Two representations are considered different if their terms are in a different order or if the integer being squared (not just the square) is different.
Input : n=1
12 + 02 + 02 + 02
02 + 12 + 02 + 02
02 + 02 + 12 + 02
02 + 02 + 02 + 12
Similarly there are 4 other possible perumutations by replacing 1 with -1
Hence there are 8 possible ways.
Jacobi’s four-square theorem states that the number of ways of writing n as a sum of 4 squares is 8 times the sum of divisor of n if n is odd and is 24 times the sum of odd divisor of n if n is even.Find the sum of odd and even divisor of n by running a loop from 1 to sqrt(n) .
Time Complexity : O(sqrt(N))
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum number of squares whose sum equals to given number n
- Minimum number of squares whose sum equals to given number N | set 2
- Count number of squares in a rectangle
- Paper Cut into Minimum Number of Squares
- Check whether a number can be represented by sum of two squares
- Square pyramidal number (Sum of Squares)
- Number of perfect squares between two given numbers
- Number of squares of maximum area in a rectangle
- Count number less than N which are product of perfect squares
- Sum of the count of number of adjacent squares in an M X N grid
- Check whether a number can be represented by the product of two squares
- Check whether a number can be represented as difference of two squares
- Puzzle | Program to find number of squares in a chessboard
- Find the number of squares inside the given square grid
- Number of unique rectangles formed using N unit squares
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.