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
12 + 02 + 02 + 02
02 + 12 + 02 + 02
02 + 02 + 12 + 02
02 + 02 + 02 + 12
Similarly there are 4 other possible permutations 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++
#include <bits/stdc++.h>
using namespace std;
int sum_of_4_squares( int n)
{
int i, odd = 0, even = 0;
for (i = 1; i <= sqrt (n); i++) {
if (n % i == 0) {
if (i % 2 == 0)
even += i;
else
odd += i;
if ((n / i) != i) {
if ((n / i) % 2 == 0)
even += (n / i);
else
odd += (n / i);
}
}
}
if (n % 2 == 1)
return 8 * (odd + even);
else
return 24 * (odd);
}
int main()
{
int n = 4;
cout << sum_of_4_squares(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int sum_of_4_squares( int n)
{
int i, odd = 0 , even = 0 ;
for (i = 1 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (i % 2 == 0 )
even += i;
else
odd += i;
if ((n / i) != i)
{
if ((n / i) % 2 == 0 )
even += (n / i);
else
odd += (n / i);
}
}
}
if (n % 2 == 1 )
return 8 * (odd + even);
else
return 24 * (odd);
}
public static void main (String[] args)
{
int n = 4 ;
System.out.println (sum_of_4_squares(n));
}
}
|
Python3
def sum_of_4_squares(n):
i, odd, even = 0 , 0 , 0
for i in range ( 1 , int (n * * (. 5 )) + 1 ):
if (n % i = = 0 ):
if (i % 2 = = 0 ):
even + = i
else :
odd + = i
if ((n / / i) ! = i):
if ((n / / i) % 2 = = 0 ):
even + = (n / / i)
else :
odd + = (n / / i)
if (n % 2 = = 1 ):
return 8 * (odd + even)
else :
return 24 * (odd)
n = 4
print (sum_of_4_squares(n))
|
C#
using System;
class GFG
{
static int sum_of_4_squares( int n)
{
int i, odd = 0, even = 0;
for (i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (i % 2 == 0)
even += i;
else
odd += i;
if ((n / i) != i)
{
if ((n / i) % 2 == 0)
even += (n / i);
else
odd += (n / i);
}
}
}
if (n % 2 == 1)
return 8 * (odd + even);
else
return 24 * (odd);
}
static public void Main ()
{
int n = 4;
Console.WriteLine(sum_of_4_squares(n));
}
}
|
Javascript
<script>
function sum_of_4_squares(n)
{
var i, odd = 0, even = 0;
for (i = 1; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
if (i % 2 == 0)
even += i;
else
odd += i;
if ((n / i) != i)
{
if ((n / i) % 2 == 0)
even += (n / i);
else
odd += (n / i);
}
}
}
if (n % 2 == 1)
return 8 * (odd + even);
else
return 24 * (odd);
}
var n = 4;
document.write(sum_of_4_squares(n));
</script>
|
Time Complexity : O(sqrt(N))
Auxiliary Space: O(1)
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...