📌  相关文章
📜  平方差等于N的整数对的计数

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

给定正整数N ,任务是找到平方差等于N的整数对(x,y)的数量,即

x^{2} - y^{2} = N

例子:

方法:
给定的等式也可以写成:

现在对于给定方程的积分解:

令(x + y)= p1和(x + y)= p2
是两个方程,其中p1和p2是N的因数
这样p1 * p2 = N。

解决以上两个方程式,我们有:

根据以上计算,要使x和y为整数,则除数之和必须为偶数。由于x和y的两个值有(+ x,+ y),(+ x,-y),(-x,+ y)和(-x,-y)有4个可能值。
因此,可能的解决方案总数由4 *(成对的偶数个除数对)得出。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the integral
// solutions of the given equation
void findSolutions(int N)
{
 
    // Initialise count to 0
    int count = 0;
 
    // Iterate till sqrt(N)
    for (int i = 1; i <= sqrt(N); i++) {
 
        if (N % i == 0) {
 
            // If divisor's pair sum is even
            if ((i + N / i) % 2 == 0) {
                count++;
            }
        }
    }
 
    // Print the total possible solutions
    cout << 4 * count << endl;
}
 
// Driver Code
int main()
{
    // Given number N
    int N = 80;
 
    // Function Call
    findSolutions(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the integral
// solutions of the given equation
static void findSolutions(int N)
{
 
    // Initialise count to 0
    int count = 0;
 
    // Iterate till sqrt(N)
    for(int i = 1; i <= Math.sqrt(N); i++)
    {
       if (N % i == 0)
       {
            
           // If divisor's pair sum is even
           if ((i + N / i) % 2 == 0)
           {
               count++;
           }
       }
    }
     
    // Print the total possible solutions
    System.out.print(4 * count);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given number N
    int N = 80;
     
    // Function Call
    findSolutions(N);
}
}
 
// This code is contributed by Shubham Prakash.


Python3
# Python3 program for the above approach
import math;
 
# Function to find the integral
# solutions of the given equation
def findSolutions(N):
 
    # Initialise count to 0
    count = 0;
 
    # Iterate till sqrt(N)
    for i in range(1, int(math.sqrt(N)) + 1):
 
        if (N % i == 0):
 
            # If divisor's pair sum is even
            if ((i + N // i) % 2 == 0):
                count += 1;
             
    # Print the total possible solutions
    print(4 * count);
 
# Driver Code
 
# Given number N
N = 80;
 
# Function Call
findSolutions(N);
 
# This code is contributed by Code_Mech


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find the integral
// solutions of the given equation
static void findSolutions(int N)
{
 
    // Initialise count to 0
    int count = 0;
 
    // Iterate till sqrt(N)
    for(int i = 1; i <= Math.Sqrt(N); i++)
    {
        if (N % i == 0)
        {
                 
            // If divisor's pair sum is even
            if ((i + N / i) % 2 == 0)
            {
                count++;
            }
        }
    }
     
    // Print the total possible solutions
    Console.Write(4 * count);
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given number N
    int N = 80;
     
    // Function Call
    findSolutions(N);
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
12

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