📜  距原点距离 D 的积分点数

📅  最后修改于: 2022-05-13 01:56:04.734000             🧑  作者: Mango

距原点距离 D 的积分点数

给定一个正整数D ,任务是找到距离原点D整数坐标(x, y)的数量。

例子:

方法:这个问题可以简化为计算位于以原点为中心、半径为D的圆的圆周上的整数坐标,并且可以借助毕达哥拉斯定理来解决。由于这些点应该与原点的距离为D ,所以它们都必须满足方程x * x + y * y = D 2其中 (x, y) 是该点的坐标。
现在,要解决上述问题,请按照以下步骤操作:

  • 初始化一个变量,比如count ,它存储可能的坐标对的总数。
  • 遍历所有可能的x 坐标并计算y的对应值作为sqrt(D 2 – y*y)
  • 因为每个xy都是正整数的坐标可以形成总共4 个可能的有效对,分别是{x, y}、{-x, y}、{-x, -y}、{x, -y}和 increment在变量count中将每个可能的对(x, y)计数为4
  • 此外,由于圆的半径是整数,所以圆的圆周上总是存在一个整数坐标,它与 x 轴和 y 轴相交。所以在count中加 4 来补偿这些点。
  • 完成上述步骤后,将count的值打印为结果对数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the total valid
// integer coordinates at a distance D
// from origin
int countPoints(int D)
{
    // Stores the count of valid points
    int count = 0;
 
    // Iterate over possible x coordinates
    for (int x = 1; x * x < D * D; x++) {
 
        // Find the respective y coordinate
        // with the pythagoras theorem
        int y = (int)sqrt(double(D * D - x * x));
        if (x * x + y * y == D * D) {
            count += 4;
        }
    }
 
    // Adding 4 to compensate the coordinates
    // present on x and y axes.
    count += 4;
 
    // Return the answer
    return count;
}
 
// Driver Code
int main()
{
    int D = 5;
    cout << countPoints(D);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
// Function to find the total valid
// integer coordinates at a distance D
// from origin
static int countPoints(int D)
{
   
    // Stores the count of valid points
    int count = 0;
 
    // Iterate over possible x coordinates
    for (int x = 1; x * x < D * D; x++) {
 
        // Find the respective y coordinate
        // with the pythagoras theorem
        int y = (int)Math.sqrt((D * D - x * x));
        if (x * x + y * y == D * D) {
            count += 4;
        }
    }
 
    // Adding 4 to compensate the coordinates
    // present on x and y axes.
    count += 4;
 
    // Return the answer
    return count;
}
 
// Driver Code
public static void main (String[] args)
{
    int D = 5;
    System.out.println(countPoints(D));
}
}
 
// this code is contributed by shivanisinghss2110


Python3
# python 3 program for the above approach
from math import sqrt
 
# Function to find the total valid
# integer coordinates at a distance D
# from origin
def countPoints(D):
   
    # Stores the count of valid points
    count = 0
 
    # Iterate over possible x coordinates
    for x in range(1, int(sqrt(D * D)), 1):
 
        # Find the respective y coordinate
        # with the pythagoras theorem
        y = int(sqrt((D * D - x * x)))
        if (x * x + y * y == D * D):
            count += 4
 
    # Adding 4 to compensate the coordinates
    # present on x and y axes.
    count += 4
 
    # Return the answer
    return count
 
# Driver Code
if __name__ == '__main__':
    D = 5
    print(countPoints(D))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
// Function to find the total valid
// integer coordinates at a distance D
// from origin
public class GFG{
    static int countPoints(int D){
         
        // Stores the count of valid points
        int count = 0;
         
        // Iterate over possible x coordinates
        for(int x = 1; x*x < D*D; x++){
            int y = (int)Math.Sqrt((D * D - x * x));
 
            // Find the respective y coordinate
            // with the pythagoras theorem
            if(x * x + y * y == D * D){
              count += 4;  
            }
       }
    // Adding 4 to compensate the coordinates
    // present on x and y axes.
     
    count += 4;
 
    // Return the answer
    return count;
}
     
    // Driver Code
 
    public static void Main(){
        int D = 5;
        Console.Write(countPoints(D));
    }
}
 
// This code is contributed by gfgking


Javascript


输出
12

时间复杂度: O(R)
辅助空间: O(1)