给定数字N。任务是对N下的总数进行计数,这些总数既是完美的平方,又是一些整数的立方。
例子:
Input: N = 100
Output: 2
They are 1 and 64.
Input: N = 100000
Output: 6
方法:对于给定的正数N是一个完美的平方,它必须满足P 2 = N类似地,对于一个理想的立方体,其中P和Q是一些正整数,Q 3 =N。
N = P 2 = Q 3
因此,如果N是6的幂,那么这肯定会起作用。说N = A 6 ,可以写为(A 3 ) 2或(A 2 ) 3 。
因此,请选择小于N的每个正整数的6幂。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
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
Javascript
输出:
6
时间复杂度: O(N 1/6 )
辅助空间: O(1)