Given a number N, the task is to count all ‘a’ and ‘b’ that satisfy the condition a^2 + b^2 = N.
Note:- (a, b) and (b, a) are to be considered as two different pairs and (a, a) is also valid and to be considered only one time.
Input: N = 10 Output: 2 1^2 + 3^2 = 9 3^2 + 1^2 = 9 Input: N = 8 Output: 1 2^2 + 2^2 = 8
- Traverse numbers from 1 to square root of N.
- Subtract square of the current number from N and check if their difference is a perfect square or not.
- If it is perfect square then increment the count.
- Return count.
Below is the implementation of above approach:
For n = 1, 1 pair exists For n = 2, 1 pair exists For n = 3, 0 pair exists For n = 4, 1 pair exists For n = 5, 2 pair exists For n = 6, 0 pair exists For n = 7, 0 pair exists For n = 8, 1 pair exists For n = 9, 1 pair exists For n = 10, 2 pair exists
Time Complexity : O(sqrt(N))
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Count Magic squares in a grid
- Count of perfect squares of given length
- Count number of squares in a rectangle
- Count squares with odd side length in Chessboard
- Count of squares that can be drawn without lifting the pencil
- Count number less than N which are product of perfect squares
- Sum of the count of number of adjacent squares in an M X N grid
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Count the total number of squares that can be visited by Bishop in one move
- Program to count number of distinct Squares and Cubes upto N
- Count pairs with Odd XOR
- Count pairs with given sum | Set 2
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Count of pairs (x, y) in an array such that x < y
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.