# Count number less than N which are product of perfect squares

Given an Integer N. The task is count numbers P less than N such that P is a product of two distinct perfect squares.

**Examples**:

Input: N = 36Output: 5 Numbers are 4 = 1^{2}* 2^{2}, 9 = 1^{2}* 3^{2}, 16 = 1^{2}* 4^{2}, 25 = 1^{2}* 5^{2}, 36 = 1^{2}* 6^{2}Input: N = 1000000Output: 999

**Approach:** Let us consider a number P = (a^{2} * b^{2}) such that P <= N. So we have (a^{2} * b^{2}) <= 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 (a^{2} * b^{2}) <= N.

Below is the implementation of the above approach:

## C++

`// C++ program to count number less ` `// than N which are product of ` `// any two perfect squares ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count number less ` `// than N which are product of ` `// any two perfect squares ` `int` `countNumbers(` `int` `N) ` `{ ` ` ` `return` `int` `(` `sqrt` `(N)) - 1; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `N = 36; ` ` ` ` ` `cout << countNumbers(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number less ` `// than N which are product of ` `// any two perfect squares ` `import` `java.util.*; ` ` ` ` ` `class` `solution ` `{ ` ` ` `// Function to count number less ` `// than N which are product of ` `// any two perfect squares ` `static` `int` `countNumbers(` `int` `N) ` `{ ` ` ` `return` `(` `int` `)Math.sqrt(N) - ` `1` `; ` `} ` ` ` `// Driver program ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `N = ` `36` `; ` ` ` ` ` `System.out.println(countNumbers(N)); ` ` ` `} ` ` ` `} ` ` ` `//This code is contributed by ` `// Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to count number ` `# less than N which are product of ` `# any two perfect squares ` `import` `math ` ` ` `# Function to count number less ` `# than N which are product of ` `# any two perfect squares ` `def` `countNumbers(N): ` ` ` `return` `int` `(math.sqrt(N)) ` `-` `1` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `N ` `=` `36` ` ` ` ` `print` `(countNumbers(N)) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number less ` `// than N which are product of ` `// any two perfect squares ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to count number less ` `// than N which are product of ` `// any two perfect squares ` `static` `int` `countNumbers(` `int` `N) ` `{ ` ` ` `return` `(` `int` `)(Math.Sqrt(N)) - 1; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 36; ` ` ` ` ` `Console.Write(countNumbers(N)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number less ` `// than N which are product of ` `// any two perfect squares ` ` ` `// Function to count number less ` `// than N which are product of ` `// any two perfect squares ` `function` `countNumbers(` `$N` `) ` `{ ` ` ` `return` `(int)(sqrt(` `$N` `)) - 1; ` `} ` ` ` `// Driver Code ` `$N` `= 36; ` `echo` `countNumbers(` `$N` `); ` ` ` `// This code is contributed by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity:** O(log(N))

## Recommended Posts:

- Number of perfect squares between two given numbers
- Count of pairs in an array whose product is a perfect square
- Count number of squares in a rectangle
- Count all perfect divisors of a number
- Sum of the count of number of adjacent squares in an M X N grid
- Print all perfect squares from the given range
- Count numbers upto N which are both perfect square and perfect cube
- 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
- Sum of all Perfect Squares lying in the range [L, R] for Q queries
- Smallest and Largest N-digit perfect squares
- Check if the sum of perfect squares in an array is divisible by x
- Sort perfect squares in an array at their relative positions
- Sum of distances between the two nearest perfect squares to all the nodes of the given linked list

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.