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
- Count number less than N which are product of perfect squares
- Count squares with odd side length in Chessboard
- 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 of pairs from 1 to a and 1 to b whose sum is divisible by N
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized
- Count Pairs from two arrays with even sum
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N)
- Count pairs (i,j) such that (i+j) is divisible by A and B both
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
- Count of pairs (x, y) in an array such that x < y
- Count pairs with set bits sum equal to K
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.