Count numbers upto N which are both perfect square and perfect cube
Given a number N. The task is to count total numbers under N which are both perfect square and cube of some integers.
Examples:
Input: N = 100 Output: 2 They are 1 and 64. Input: N = 100000 Output: 6
Naive Approach: The idea is to use the power, square_root and cube_root functions from the math library.
C++
// C++ program for the above approach #include <cmath> #include <iostream> // Function to count the perfect square // and cubes int countPerfectSquaresCubes( int N) { int count = 0; // Iterate over the range [1, N] for ( int i = 1; i <= N; i++) { if (std:: pow (( int )std:: sqrt (i), 2) == i && std:: pow (( int )std::cbrt(i), 3) == i) { count++; } } // Return the resultant count return count; } // Driver Code int main() { int count = countPerfectSquaresCubes(100000); std::cout << count << std::endl; return 0; } // This code is contributed by aeroabrar_31 |
Java
// Java program for the above approach import java.util.*; class GFG { // Function to count the perfect square // and cubes public static int countPerfectSquaresCubes( int N) { int count = 0 ; for ( int i = 1 ; i <= N; i++) { if (Math.pow(( int )Math.sqrt(i), 2 ) == i && Math.pow(( int )Math.cbrt(i), 3 ) == i) { count++; } } // Return the resultant count return count; } // Driver Code public static void main(String[] args) { int count = countPerfectSquaresCubes( 100000 ); System.out.println(count); } } |
C#
// C# program for the above approach using System; public class GFG { // Function to count the perfect square // and cubes public static int countPerfectSquaresCubes( int N) { int count = 0; for ( int i = 1; i <= N; i++) { if (Math.Pow(( int )Math.Sqrt(i), 2) == i && Math.Pow(( int )Math.Cbrt(i), 3) == i) { count++; } } // Return the resultant count return count; } // Driver Code public static void Main() { int count = countPerfectSquaresCubes(100000); Console.WriteLine(count); } } // This code is contributed by aeroabrar_31 |
6
Time Complexity: O(N*(logN))
Space Complexity: O(1)
Method 2 : Optimal
Approach: For a given positive number N to be a perfect square, it must satisfy P2 = N Similarly, Q3 = N for a perfect cube where P and Q are some positive integers.
N = P2 = Q3
Thus, if N is a 6th power, then this would certainly work. Say N = A6 which can be written as (A3)2 or (A2)3.
So, pick 6th power of every positive integers which are less than equal to N.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to return required count int SquareCube( long long int N) { int cnt = 0, i = 1; while ( int ( pow (i, 6)) <= N) { ++cnt; ++i; } return cnt; } int main() { long long int N = 100000; // function call to print required answer cout << SquareCube(N); return 0; } |
Java
// Java implementation of the above approach public class GFG{ // Function to return required count static int SquareCube( long N) { int cnt = 0 , i = 1 ; while (( int )(Math.pow(i, 6 )) <= N) { ++cnt; ++i; } return cnt; } public static void main(String []args) { long N = 100000 ; // function call to print required answer System.out.println(SquareCube(N)) ; } // This code is contributed by Ryuga } |
Python3
# Python3 implementation of the # above approach # Function to return required count def SquareCube( N): cnt, i = 0 , 1 while (i * * 6 < = N): cnt + = 1 i + = 1 return cnt # Driver code N = 100000 # function call to print required # answer print (SquareCube(N)) # This code is contributed # by saurabh_shukla |
C#
// C# implementation of the above approach using System; public class GFG{ // Function to return required count static int SquareCube( long N) { int cnt = 0, i = 1; while (( int )(Math.Pow(i, 6)) <= N) { ++cnt; ++i; } return cnt; } public static void Main() { long N = 100000; // function call to print required answer Console.WriteLine(SquareCube(N)) ; } } /*This code is contributed by 29AjayKumar*/ |
PHP
<?php // PHP implementation of the // above approach // Function to return required count function SquareCube( $N ) { $cnt = 0; $i = 1; while ((pow( $i , 6)) <= $N ) { ++ $cnt ; ++ $i ; } return $cnt ; } // Driver Code $N = 100000; // function call to print required answer echo SquareCube( $N ); // This code is contributed by ita_c ?> |
Javascript
<script> // JavaScript implementation of the above approach // Function to return required count function SquareCube(N) { let cnt = 0, i = 1; while (Math.floor(Math.pow(i, 6)) <= N) { ++cnt; ++i; } return cnt; } let N = 100000; // function call to print required answer document.write(SquareCube(N)); // This code is contributed by Surbhi Tyagi. </script> |
6
Time Complexity: O(N1/6)
Auxiliary Space: O(1), since no extra space has been taken.
Please Login to comment...