📜  最小和最大N位完美立方体

📅  最后修改于: 2021-04-27 20:34:31             🧑  作者: Mango

给定一个整数N ,任务是找到最小和最大的N个数字,它们也是理想的立方体。
例子:

方法:为了提高从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