📜  圆点和格点

📅  最后修改于: 2021-10-23 09:07:36             🧑  作者: Mango

给定一个以原点或 (0, 0) 为中心的二维半径为r的圆。任务是找到圆周上的总格点。点阵点是在二维空间中坐标为整数的点。

例子:

Input  : r = 5.
Output : 12
Below are lattice points on a circle with
radius 5 and origin as (0, 0).
(0,5), (0,-5), (5,0), (-5,0),
(3,4), (-3,4), (-3,-4), (3,-4),
(4,3), (-4,3), (-4,-3), (4,-3).
are 12 lattice point.

为了找到格点,我们基本上需要找到满足方程 x 2 + y 2 = r 2的 (x, y) 值。
对于满足上述方程的任何 (x, y) 值,我们实际上总共有 4 种不同的组合满足方程。例如,如果 r = 5 且 (3, 4) 是满足方程的对,则实际上有 4 种组合 (3, 4) , (-3,4) , (-3,-4) , (3,- 4)。但是有一个例外,在 (0, r) 或 (r, 0) 的情况下,实际上有 2 个点,因为没有负 0。

// Initialize result as 4 for (r, 0), (-r. 0),
// (0, r) and (0, -r)
result = 4

Loop for x = 1 to r-1 and do following for every x.
    If r*r - x*x is a perfect square, then add 4 
    tor result.  

下面是上述想法的实现。

CPP
// C++ program to find countLattice points on a circle
#include
using namespace std;
  
// Function to count Lattice points on a circle
int countLattice(int r)
{
    if (r <= 0)
        return 0; 
  
    // Initialize result as 4 for (r, 0), (-r. 0),
    // (0, r) and (0, -r)
    int result = 4;
  
    // Check every value that can be potential x
    for (int x=1; x


Java
// Java program to find
// countLattice points on a circle
  
class GFG
{
  
// Function to count
// Lattice points on a circle
static int countLattice(int r)
{
    if (r <= 0)
        return 0; 
   
    // Initialize result as 4 for (r, 0), (-r. 0),
    // (0, r) and (0, -r)
    int result = 4;
   
    // Check every value that can be potential x
    for (int x=1; x


Python3
# Python3 program to find
# countLattice podefs on a circle
  
import math
  
# Function to count Lattice
# podefs on a circle
def countLattice(r):
  
    if (r <= 0):
        return 0  
  
    # Initialize result as 4 for (r, 0), (-r. 0),
    # (0, r) and (0, -r)
    result = 4 
  
    # Check every value that can be potential x
    for x in range(1, r):
      
        # Find a potential y
        ySquare = r*r - x*x 
        y = int(math.sqrt(ySquare)) 
  
        # checking whether square root is an defeger
        # or not. Count increments by 4 for four 
        # different quadrant values
        if (y*y == ySquare):
            result += 4 
       
  
    return result 
   
  
# Driver program
r = 5 
print(countLattice(r)) 
  
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program to find countLattice
// points on a circle
using System;
  
class GFG {
  
    // Function to count Lattice
    // points on a circle
    static int countLattice(int r)
    {
        if (r <= 0)
            return 0; 
      
        // Initialize result as 4
        // for (r, 0), (-r. 0),
        // (0, r) and (0, -r)
        int result = 4;
      
        // Check every value that
        // can be potential x
        for (int x = 1; x < r; x++)
        {
              
            // Find a potential y
            int ySquare = r*r - x*x;
            int y = (int)Math.Sqrt(ySquare);
      
            // checking whether square root
            // is an integer or not. Count
            // increments by 4 for four 
            // different quadrant values
            if (y*y == ySquare)
                result += 4;
        }
      
        return result;
    }
      
    // Driver code
    public static void Main() 
    {
        int r = 5;
          
        Console.Write(countLattice(r));
    }
}
  
// This code is contributed by nitin mittal.


PHP


输出:

12

参考:
http://mathworld.wolfram.com/CircleLatticePoints.html

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程