Count of integral points that lie at a distance D from origin
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
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 centred 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 originint 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 Codeint main(){ int D = 5; cout << countPoints(D); return 0;} |
Java
// Java program for the above approachimport java.io.*;class GFG {// Function to find the total valid// integer coordinates at a distance D// from originstatic int countPoints(int D){ // Stores the count of valid points int count = 0; // Iterate over possibel 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 Codepublic 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 approachfrom math import sqrt# Function to find the total valid# integer coordinates at a distance D# from origindef countPoints(D): # Stores the count of valid points count = 0 # Iterate over possibel 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 Codeif __name__ == '__main__': D = 5 print(countPoints(D)) # This code is contributed by SURENDRA_GANGWAR. |
C#
// C# program for the above approachusing System;// Function to find the total valid// integer coordinates at a distance D// from originpublic class GFG{ static int countPoints(int D){ // Stores the count of valid points int count = 0; // Iterate over possibel 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 possibel 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> |
12
Time Complexity: O(R)
Auxiliary Space: O(1)



