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++
#include <cmath>
#include <iostream>
int countPerfectSquaresCubes( int N)
{
int count = 0;
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 count;
}
int main()
{
int count = countPerfectSquaresCubes(100000);
std::cout << count << std::endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
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 count;
}
public static void main(String[] args)
{
int count = countPerfectSquaresCubes( 100000 );
System.out.println(count);
}
}
|
Python3
import math
def countPerfectSquaresCubes(N):
count = 0
for i in range ( 1 , N + 1 ):
if int (math.sqrt(i)) * * 2 = = i and int ( round (math. pow (i, 1 / 3 ), 2 )) * * 3 = = i:
count + = 1
return count
if __name__ = = '__main__' :
count = countPerfectSquaresCubes( 100000 )
print (count)
|
C#
using System;
public class GFG {
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 count;
}
public static void Main()
{
int count = countPerfectSquaresCubes(100000);
Console.WriteLine(count);
}
}
|
Javascript
function countPerfectSquaresCubes(N) {
let count = 0;
for (let i = 1; i <= N; i++) {
if (Math.pow(Math.floor(Math.sqrt(i)), 2) === i &&
Math.pow(Math.floor(Math.cbrt(i)), 3) === i) {
count++;
}
}
return count;
}
const count = countPerfectSquaresCubes(100000);
console.log(count);
|
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++
#include <bits/stdc++.h>
using namespace std;
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;
cout << SquareCube(N);
return 0;
}
|
Java
public class GFG{
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 ;
System.out.println(SquareCube(N)) ;
}
}
|
Python3
def SquareCube( N):
cnt, i = 0 , 1
while (i * * 6 < = N):
cnt + = 1
i + = 1
return cnt
N = 100000
print (SquareCube(N))
|
C#
using System;
public class GFG{
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;
Console.WriteLine(SquareCube(N)) ;
}
}
|
Javascript
<script>
function SquareCube(N)
{
let cnt = 0, i = 1;
while (Math.floor(Math.pow(i, 6)) <= N)
{
++cnt;
++i;
}
return cnt;
}
let N = 100000;
document.write(SquareCube(N));
</script>
|
PHP
<?php
function SquareCube( $N )
{
$cnt = 0;
$i = 1;
while ((pow( $i , 6)) <= $N )
{
++ $cnt ;
++ $i ;
}
return $cnt ;
}
$N = 100000;
echo SquareCube( $N );
?>
|
Time Complexity: O(N1/6)
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
02 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...