Given an Integer N. The task is count numbers P less than N such that P is a product of two distinct perfect squares.
Input : N = 36 Output : 5 Numbers are 4 = 12 * 22, 9 = 12 * 32, 16 = 12 * 42, 25 = 12 * 52, 36 = 12 * 62 Input : N = 1000000 Output : 999
Approach: Let us consider a number P = (a2 * b2) such that P <= N. So we have (a2 * b2) <= N. This can be written as (a * b) <= sqrt(N).
So we have to count pairs (a, b) such that (a * b) <= sqrt(N) and a <= b.
Let us take a number Q = (a * b) such that Q <= sqrt(N).
Taking a = 1, we have b = sqrt(N) – 1 numbers such that, ( a * b = Q <= sqrt(N)).
Thus we can have all sqrt(N) – 1 numbers such that (a2 * b2) <= N.
Below is the implementation of the above approach:
Time Complexity: O(log(N))
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.
- Subsets of size K with product equal to difference of two perfect squares
- Count of perfect squares of given length
- Count of primes in a given range that can be expressed as sum of perfect squares
- Count elements in an Array that can be represented as difference of two perfect squares
- Number of perfect squares between two given numbers
- Count of pairs in an array whose product is a perfect square
- Check whether a number can be represented by the product of two squares
- Count number of squares in a rectangle
- Sum of the count of number of adjacent squares in an M X N grid
- Count all perfect divisors of a number
- Print all perfect squares from the given range
- 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
- Largest sub-array whose all elements are perfect squares
- Smallest and Largest N-digit perfect squares
- Check if the sum of perfect squares in an array is divisible by x
- Sum of all Perfect Squares lying in the range [L, R] for Q queries
- Count the nodes in the given Tree whose weight is a Perfect Number
- Count numbers upto N which are both perfect square and perfect cube
- Sort perfect squares in an array at their relative positions
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.