圆和点阵点

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

null
Input  : r = 5.Output : 12Below are lattice points on a circle withradius 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的(x,y)值 2. +y 2. =r 2. . 对于满足上述方程的(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 = 4Loop 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<bits/stdc++.h>
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<r; x++)
{
// Find a potential y
int ySquare = r*r - x*x;
int y = 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 program
int main()
{
int r = 5;
cout << countLattice(r);
return 0;
}


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<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(String arg[])
{
int r = 5 ;
System.out.println(countLattice(r));
}
}
// This code is contributed by Anant Agarwal.


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

<?php
// PHP program to find countLattice
// points on a circle
// Function to count Lattice
// points on a circle
function 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 = 1; $x < $r ; $x ++)
{
// Find a potential y
$ySquare = $r * $r - $x * $x ;
$y = ceil (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
$r = 5;
echo countLattice( $r );
// This code is contributed by nitin mittal
?>


Javascript

<script>
// javascript program to find
// countLattice points on a circle
// Function to count
// Lattice points on a circle
function countLattice(r) {
if (r <= 0)
return 0;
// Initialize result as 4 for (r, 0), (-r. 0),
// (0, r) and (0, -r)
var result = 4;
// Check every value that can be potential x
for (x = 1; x < r; x++)
{
// Find a potential y
var ySquare = r * r - x * x;
var y = parseInt( 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
var r = 5;
document.write(countLattice(r));
// This code is contributed by umadevi9616
</script>


输出:

12

参考: http://mathworld.wolfram.com/CircleLatticePoints.html 本文由 希瓦姆·普拉丹(anuj_charm) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享