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))
- Count number of squares in a rectangle
- Count Magic squares in a grid
- Sum of the count of number of adjacent squares in an M X N grid
- Count number less than N which are product of perfect squares
- Count squares with odd side length in Chessboard
- Count of squares that can be drawn without lifting the pencil
- 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 of pairs of (i, j) such that ((n % i) % j) % n is maximized
- Count of odd and even sum pairs in an array
- Count number of pairs (i, j) such that arr[i] * arr[j] = arr[i] + arr[j]
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N)
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.