给定一个整数N ,任务是找到最小和最大的N个数字,它们也是理想的立方体。
例子:
Input: N = 2
Output: 27 64
27 and 64 are the smallest and the largest 2-digit numbers which are also perfect cubes.
Input: N = 3
Output: 125 729
方法:为了提高从N = 1开始的N个值,该系列将继续像8,64,729,9261,… ..为最大的N位完美的立方体,其第N个任期将是POW(小区(CBRT( pow(10,(n))))-1,3) 。
和1,27,125,1000 ,……对于最小的N位完美立方,其第N个项将是pow(ceil(cbrt(pow(10,(n – 1)))),3) 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to print the largest and
// the smallest n-digit perfect cube
void nDigitPerfectCubes(int n)
{
// Smallest n-digit perfect cube
cout << pow(ceil(cbrt(pow(10, (n - 1)))), 3) << " ";
// Largest n-digit perfect cube
cout << (int)pow(ceil(cbrt(pow(10, (n)))) - 1, 3);
}
// Driver code
int main()
{
int n = 3;
nDigitPerfectCubes(n);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to print the largest and
// the smallest n-digit perfect cube
static void nDigitPerfectCubes(int n)
{
// Smallest n-digit perfect cube
int smallest = (int)Math.pow(Math.ceil(Math.cbrt(Math.pow(10, (n - 1)))), 3);
System.out.print(smallest + " ");
int largest = (int)Math.pow(Math.ceil(Math.cbrt(Math.pow(10, (n)))) - 1, 3);
System.out.print(largest);
}
// Driver code
public static void main(String args[])
{
int n = 3;
nDigitPerfectCubes(n);
}
}
Python3
# Python3 implementation of the approach
from math import ceil
# Function to print the largest and
# the smallest n-digit perfect cube
def nDigitPerfectCubes(n):
# Smallest n-digit perfect cube
print(pow(ceil((pow(10, (n - 1))) **
(1 / 3)), 3), end = " ")
# Largest n-digit perfect cube
print(pow(ceil((pow(10, (n))) **
(1 / 3)) - 1, 3))
# Driver code
if __name__ == "__main__":
n = 3
nDigitPerfectCubes(n)
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to print the largest and
// the smallest n-digit perfect cube
static void nDigitPerfectCubes(int n)
{
// Smallest n-digit perfect cube
int smallest = (int)Math.Pow(Math.Ceiling(MathF.Cbrt((float)Math.Pow(10, (n - 1)))), 3);
Console.Write(smallest + " ");
int largest = (int)Math.Pow(Math.Ceiling(MathF.Cbrt((float)Math.Pow(10, (n)))) - 1, 3);
Console.Write(largest);
}
// Driver code
static void Main()
{
int n = 3;
nDigitPerfectCubes(n);
}
}
// This code is contributed by mits
PHP
Javascript
输出:
125 729