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++ 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 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
# 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# 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
<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>
Output
12
Time Complexity: O(R)
Auxiliary Space: O(1)