Count of integral points that lie at a distance D from origin
Last Updated :
13 Jun, 2022
Given a positive integer D, the task is to find the number of integer coordinates (x, y) which lie at a distance D from origin.
Example:
Input: D = 1
Output: 4
Explanation: Total valid points are {1, 0}, {0, 1}, {-1, 0}, {0, -1}
Input: D = 5
Output: 12
Explanation: Total valid points are {0, 5}, {0, -5}, {5, 0}, {-5, 0}, {3, 4}, {3, -4}, {-3, 4}, {-3, -4}, {4, 3}, {4, -3}, {-4, 3}, {-4, -3}
Approach: This question can be simplified to count integer coordinates lying on the circumference of the circle centered at the origin, having a radius D and can be solved with the help of the Pythagoras theorem. As the points should be at a distance D from the origin, so they all must satisfy the equation x * x + y * y = D2 where (x, y) are the coordinates of the point.
Now, to solve the above problem, follow the below steps:
- Initialize a variable, say count that stores the total count of the possible pairs of coordinates.
- Iterate over all possible x coordinates and calculate the corresponding value of y as sqrt(D2 – y*y).
- Since every coordinate whose both x and y are positive integers can form a total of 4 possible valid pairs as {x, y}, {-x, y}, {-x, -y}, {x, -y} and increment the count each possible pair (x, y) by 4 in the variable count.
- Also, there is always an integer coordinate present at the circumference of the circle where it cuts the x-axis and y-axis because the radius of the circle is an integer. So add 4 in count, to compensate these points.
- After completing the above steps, print the value of count as the resultant count of pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPoints( int D)
{
int count = 0;
for ( int x = 1; x * x < D * D; x++) {
int y = ( int ) sqrt ( double (D * D - x * x));
if (x * x + y * y == D * D) {
count += 4;
}
}
count += 4;
return count;
}
int main()
{
int D = 5;
cout << countPoints(D);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countPoints( int D)
{
int count = 0 ;
for ( int x = 1 ; x * x < D * D; x++) {
int y = ( int )Math.sqrt((D * D - x * x));
if (x * x + y * y == D * D) {
count += 4 ;
}
}
count += 4 ;
return count;
}
public static void main (String[] args)
{
int D = 5 ;
System.out.println(countPoints(D));
}
}
|
Python3
from math import sqrt
def countPoints(D):
count = 0
for x in range ( 1 , int (sqrt(D * D)), 1 ):
y = int (sqrt((D * D - x * x)))
if (x * x + y * y = = D * D):
count + = 4
count + = 4
return count
if __name__ = = '__main__' :
D = 5
print (countPoints(D))
|
C#
using System;
public class GFG{
static int countPoints( int D){
int count = 0;
for ( int x = 1; x*x < D*D; x++){
int y = ( int )Math.Sqrt((D * D - x * x));
if (x * x + y * y == D * D){
count += 4;
}
}
count += 4;
return count;
}
public static void Main(){
int D = 5;
Console.Write(countPoints(D));
}
}
|
Javascript
<script>
function countPoints(D)
{
let count = 0;
for (let x = 1; x * x < D * D; x++) {
let y = Math.floor(Math.sqrt(D * D - x * x));
if (x * x + y * y == D * D) {
count += 4;
}
}
count += 4;
return count;
}
let D = 5;
document.write(countPoints(D));
</script>
|
Time Complexity: O(R)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...