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++
// C++ program for the above approach
#include <bits/stdc++.h>
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
<script>
// JavaScript Program to implement
// the above approach
// Function to find the total valid
// integer coordinates at a distance D
// from origin
function countPoints(D)
{
// Stores the count of valid points
let count = 0;
// Iterate over possible x coordinates
for (let x = 1; x * x < D * D; x++) {
// Find the respective y coordinate
// with the pythagoras theorem
let y = Math.floor(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
let D = 5;
document.write(countPoints(D));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(R)
Auxiliary Space: O(1)
Similar Reads
Count of Disjoint Groups by grouping points that are at most K distance apart Given a 2D array arr[] and value K, where each list in arr[] represents a point on Cartesian coordinates, the task is to group the given points if the distance between them is less than or equal to K and find the total number of disjoint groups. Note: If points 'a' and 'b' are in the same group and
11 min read
Count of integral coordinates that lies inside a Square Given lower left and upper right coordinates (x1, y1) and (x2, y2) of a square, the task is to count the number of integral coordinates that lies strictly inside the square.Examples: Input: x1 = 1, y1 = 1, x2 = 5, x3 = 5 Output: 9 Explanation: Below is the square for the given coordinates: Input: x1
4 min read
Count nodes within K-distance from all nodes in a set Given an undirected tree with some marked nodes and a positive number K. We need to print the count of all such nodes which have distance from all marked nodes less than or equal to K that means every node whose distance from all marked nodes is less than or equal to K, should be counted in the resu
14 min read
Count of points such that sum of Manhattan distances is minimized Given N points in K dimensional space in a 2D array Points[][], where 1⤠N ⤠105 and 1 ⤠K ⤠5. The task is to determine the number of points (with integer coordinates) such that the sum of Manhattan distances from these points to the N points is minimized. Manhattan distance is the sum of distances
7 min read
Maximum integral co-ordinates with non-integer distances Given a maximum limit of x - coordinate and y - coordinate, we want to calculate a set of coordinates such that the distance between any two points is a non-integer number. The coordinates (i, j) chosen should be of range 0<=i<=x and 0<=j<=y. Also, we have to maximize the set. Examples:
5 min read
Count Integral points inside a Triangle Given three non-collinear integral points in XY plane, find the number of integral points inside the triangle formed by the three points. (A point in XY plane is said to be integral/lattice point if both its co-ordinates are integral). Example: Input: p = (0, 0), q = (0, 5) and r = (5,0) Output: 6Ex
8 min read