Given a circle of radius r in 2-D with origin or (0, 0) as center. The task is to find the total lattice points on circumference. Lattice Points are points with coordinates as integers in 2-D space.
Example:
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.
To find lattice points, we basically need to find values of (x, y) which satisfy the equation x2 + y2 = r2.
For any value of (x, y) that satisfies the above equation we actually have total 4 different combination which that satisfy the equation. For example if r = 5 and (3, 4) is a pair which satisfies the equation, there are actually 4 combinations (3, 4) , (-3,4) , (-3,-4) , (3,-4). There is an exception though, in case of (0, r) or (r, 0) there are actually 2 points as there is no negative 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.
Below is the implementation of above idea.
CPP
#include<bits/stdc++.h>
using namespace std;
int countLattice( int r)
{
if (r <= 0)
return 0;
int result = 4;
for ( int x=1; x<r; x++)
{
int ySquare = r*r - x*x;
int y = sqrt (ySquare);
if (y*y == ySquare)
result += 4;
}
return result;
}
int main()
{
int r = 5;
cout << countLattice(r);
return 0;
}
|
Java
class GFG
{
static int countLattice( int r)
{
if (r <= 0 )
return 0 ;
int result = 4 ;
for ( int x= 1 ; x<r; x++)
{
int ySquare = r*r - x*x;
int y = ( int )Math.sqrt(ySquare);
if (y*y == ySquare)
result += 4 ;
}
return result;
}
public static void main(String arg[])
{
int r = 5 ;
System.out.println(countLattice(r));
}
}
|
Python3
import math
def countLattice(r):
if (r < = 0 ):
return 0
result = 4
for x in range ( 1 , r):
ySquare = r * r - x * x
y = int (math.sqrt(ySquare))
if (y * y = = ySquare):
result + = 4
return result
r = 5
print (countLattice(r))
|
C#
using System;
class GFG {
static int countLattice( int r)
{
if (r <= 0)
return 0;
int result = 4;
for ( int x = 1; x < r; x++)
{
int ySquare = r*r - x*x;
int y = ( int )Math.Sqrt(ySquare);
if (y*y == ySquare)
result += 4;
}
return result;
}
public static void Main()
{
int r = 5;
Console.Write(countLattice(r));
}
}
|
PHP
<?php
function countLattice( $r )
{
if ( $r <= 0)
return 0;
$result = 4;
for ( $x = 1; $x < $r ; $x ++)
{
$ySquare = $r * $r - $x * $x ;
$y = ceil (sqrt( $ySquare ));
if ( $y * $y == $ySquare )
$result += 4;
}
return $result ;
}
$r = 5;
echo countLattice( $r );
?>
|
Javascript
<script>
function countLattice(r) {
if (r <= 0)
return 0;
var result = 4;
for (x = 1; x < r; x++)
{
var ySquare = r * r - x * x;
var y = parseInt( Math.sqrt(ySquare));
if (y * y == ySquare)
result += 4;
}
return result;
}
var r = 5;
document.write(countLattice(r));
</script>
|
Output:
12
Time Complexity: O(1)
Auxiliary Space: O(1)
Reference:
http://mathworld.wolfram.com/CircleLatticePoints.html
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Jun, 2022
Like Article
Save Article