Count number less than N which are product of perfect squares - GeeksforGeeks

10 May, 2021
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 = 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: 
C++
Java
Python 3
C#
PHP
Javascript
// 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 squaresint countNumbers(int N){    return int(sqrt(N)) - 1;} // Driver programint main(){    int N = 36;     cout << countNumbers(N);     return 0;}
// Java program to count number less// than N which are product of// any two perfect squaresimport java.util.*;  class solution{ // Function to count number less// than N which are product of// any two perfect squaresstatic int countNumbers(int N){    return (int)Math.sqrt(N) - 1;} // Driver programpublic static void main(String args[]){    int N = 36;     System.out.println(countNumbers(N));     } } //This code is contributed by// Surendra_Gangwar
# Python 3 program to count number# less than N which are product of# any two perfect squaresimport math # Function to count number less# than N which are product of# any two perfect squaresdef countNumbers(N):    return int(math.sqrt(N)) - 1 # Driver Codeif __name__ == "__main__":    N = 36     print(countNumbers(N)) # This code is contributed# by ChitraNayal
// C# program to count number less// than N which are product of// any two perfect squaresusing System; class GFG{// Function to count number less// than N which are product of// any two perfect squaresstatic int countNumbers(int N){    return (int)(Math.Sqrt(N)) - 1;} // Driver Codepublic static void Main(){    int N = 36;     Console.Write(countNumbers(N));}} // This code is contributed// by Akanksha Rai
<?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 squaresfunction countNumbers($N){    return (int)(sqrt($N)) - 1;} // Driver Code$N = 36;echo countNumbers($N); // This code is contributed by akt_mit?>
<script>    // Javascript 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 parseInt(Math.sqrt(N), 10) - 1;    }         let N = 36;       document.write(countNumbers(N));     </script>
5
 
Time Complexity: O(log(N)) 
ukasp
Akanksha_Rai
SURENDRA_GANGWAR
jit_t
decode2207
maths-perfect-square
number-theory
Mathematical
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Modular multiplicative inverse
Check if a number is Palindrome
Find first and last digits of a number
Count ways to reach the n'th stair
Program to convert a given number to words
Find Union and Intersection of two unsorted arrays