# Number of ways of writing N as a sum of 4 squares

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.

**Examples:**

Input :n=1

Output :8

1^{2}+ 0^{2}+ 0^{2}+ 0^{2}

0^{2}+ 1^{2}+ 0^{2}+ 0^{2}

0^{2}+ 0^{2}+ 1^{2}+ 0^{2}

0^{2}+ 0^{2}+ 0^{2}+ 1^{2}

Similarly there are 4 other possible perumutations by replacing 1 with -1

Hence there are 8 possible ways.

Input :n=5

Output :48

**Approach:**

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) .

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Number of ways of writing n ` `// as a sum of 4 squares ` `int` `sum_of_4_squares(` `int` `n) ` `{ ` ` ` `// sum of odd and even factor ` ` ` `int` `i, odd = 0, even = 0; ` ` ` ` ` `// iterate from 1 to the number ` ` ` `for` `(i = 1; i <= ` `sqrt` `(n); i++) { ` ` ` `// if i is the factor of n ` ` ` `if` `(n % i == 0) { ` ` ` `// if factor is even ` ` ` `if` `(i % 2 == 0) ` ` ` `even += i; ` ` ` ` ` `// if factor is odd ` ` ` `else` ` ` `odd += i; ` ` ` ` ` `// n/i is also a factor ` ` ` `if` `((n / i) != i) { ` ` ` `// if factor is even ` ` ` `if` `((n / i) % 2 == 0) ` ` ` `even += (n / i); ` ` ` ` ` `// if factor is odd ` ` ` `else` ` ` `odd += (n / i); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `// if n is odd ` ` ` `if` `(n % 2 == 1) ` ` ` `return` `8 * (odd + even); ` ` ` ` ` `// if n is even ` ` ` `else` ` ` `return` `24 * (odd); ` `} ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` ` ` `cout << sum_of_4_squares(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Number of ways of writing n ` `// as a sum of 4 squares ` `static` `int` `sum_of_4_squares(` `int` `n) ` `{ ` ` ` `// sum of odd and even factor ` ` ` `int` `i, odd = ` `0` `, even = ` `0` `; ` ` ` ` ` `// iterate from 1 to the number ` ` ` `for` `(i = ` `1` `; i <= Math.sqrt(n); i++) ` ` ` `{ ` ` ` `// if i is the factor of n ` ` ` `if` `(n % i == ` `0` `) ` ` ` `{ ` ` ` `// if factor is even ` ` ` `if` `(i % ` `2` `== ` `0` `) ` ` ` `even += i; ` ` ` ` ` `// if factor is odd ` ` ` `else` ` ` `odd += i; ` ` ` ` ` `// n/i is also a factor ` ` ` `if` `((n / i) != i) ` ` ` `{ ` ` ` `// if factor is even ` ` ` `if` `((n / i) % ` `2` `== ` `0` `) ` ` ` `even += (n / i); ` ` ` ` ` `// if factor is odd ` ` ` `else` ` ` `odd += (n / i); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `// if n is odd ` ` ` `if` `(n % ` `2` `== ` `1` `) ` ` ` `return` `8` `* (odd + even); ` ` ` ` ` `// if n is even ` ` ` `else` ` ` `return` `24` `* (odd); ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `System.out.println (sum_of_4_squares(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by tushil. ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of above approach ` ` ` `# Number of ways of writing n ` `# as a sum of 4 squares ` `def` `sum_of_4_squares(n): ` ` ` ` ` `# sum of odd and even factor ` ` ` `i, odd, even ` `=` `0` `,` `0` `,` `0` ` ` ` ` `# iterate from 1 to the number ` ` ` `for` `i ` `in` `range` `(` `1` `,` `int` `(n` `*` `*` `(.` `5` `))` `+` `1` `): ` ` ` `# if i is the factor of n ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `): ` ` ` ` ` `# if factor is even ` ` ` `if` `(i ` `%` `2` `=` `=` `0` `): ` ` ` `even ` `+` `=` `i ` ` ` ` ` `# if factor is odd ` ` ` `else` `: ` ` ` `odd ` `+` `=` `i ` ` ` ` ` `# n/i is also a factor ` ` ` `if` `((n ` `/` `/` `i) !` `=` `i): ` ` ` ` ` `# if factor is even ` ` ` `if` `((n ` `/` `/` `i) ` `%` `2` `=` `=` `0` `): ` ` ` `even ` `+` `=` `(n ` `/` `/` `i) ` ` ` ` ` `# if factor is odd ` ` ` `else` `: ` ` ` `odd ` `+` `=` `(n ` `/` `/` `i) ` ` ` ` ` ` ` ` ` `# if n is odd ` ` ` `if` `(n ` `%` `2` `=` `=` `1` `): ` ` ` `return` `8` `*` `(odd ` `+` `even) ` ` ` ` ` `# if n is even ` ` ` `else` `: ` ` ` `return` `24` `*` `(odd) ` ` ` `# Driver code ` ` ` `n ` `=` `4` ` ` `print` `(sum_of_4_squares(n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Number of ways of writing n ` `// as a sum of 4 squares ` `static` `int` `sum_of_4_squares(` `int` `n) ` `{ ` ` ` `// sum of odd and even factor ` ` ` `int` `i, odd = 0, even = 0; ` ` ` ` ` `// iterate from 1 to the number ` ` ` `for` `(i = 1; i <= Math.Sqrt(n); i++) ` ` ` `{ ` ` ` `// if i is the factor of n ` ` ` `if` `(n % i == 0) ` ` ` `{ ` ` ` `// if factor is even ` ` ` `if` `(i % 2 == 0) ` ` ` `even += i; ` ` ` ` ` `// if factor is odd ` ` ` `else` ` ` `odd += i; ` ` ` ` ` `// n/i is also a factor ` ` ` `if` `((n / i) != i) ` ` ` `{ ` ` ` `// if factor is even ` ` ` `if` `((n / i) % 2 == 0) ` ` ` `even += (n / i); ` ` ` ` ` `// if factor is odd ` ` ` `else` ` ` `odd += (n / i); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `// if n is odd ` ` ` `if` `(n % 2 == 1) ` ` ` `return` `8 * (odd + even); ` ` ` ` ` `// if n is even ` ` ` `else` ` ` `return` `24 * (odd); ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` ` ` `int` `n = 4; ` ` ` `Console.WriteLine(sum_of_4_squares(n)); ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

**Output:**

24

** Time Complexity : ** O(sqrt(N))

## Recommended Posts:

- Minimum number of squares whose sum equals to given number n
- Paper Cut into Minimum Number of Squares
- Check whether a number can be represented by sum of two squares
- Number of perfect squares between two given numbers
- Count number of squares in a rectangle
- Square pyramidal number (Sum of Squares)
- Program to find number of squares in a chessboard
- Number of squares of maximum area in a rectangle
- Count number less than N which are product of perfect squares
- Number of unique rectangles formed using N unit squares
- Count the total number of squares that can be visited by Bishop in one move
- Determine the number of squares of unit area that a given line will pass through.
- Number of squares of side length required to cover an N*M rectangle
- Program to count number of distinct Squares and Cubes upto N
- Count number of ways to divide a number in 4 parts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.