📜  N作为4平方和的书写方式数

📅  最后修改于: 2021-04-23 06:19:04             🧑  作者: Mango

给定数字N,任务是找到以4个平方的总和形式写入N的方式数。如果两个表示形式的术语顺序不同或被平方的整数(不仅仅是平方)不同,则认为这两种表示形式是不同的。

例子:

方法:
Jacobi的四平方定理指出,如果n为奇数,则将n的写法数为4个平方的和是n除数之和的8倍,如果n为偶数,则是n的奇数除数之和的24倍。通过运行从1到sqrt(n)的循环,n的奇数和偶数除数之和。

C++
// C++ implementation of above approach
#include 
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;
}


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.


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


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.


输出:
24

时间复杂度: O(sqrt(N))