# Count of integral points that lie at a distance D from origin

• Last Updated : 03 Nov, 2021

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 ``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 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 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 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 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 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

 ``
Output
`12`

Time Complexity: O(R)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up