Tuesday, November 19, 2024
Google search engine
HomeData Modelling & AISmallest and Largest N-digit perfect cubes

Smallest and Largest N-digit perfect cubes

Given an integer N, the task is to find the smallest and the largest N digit numbers which are also perfect cubes.
Examples: 
 

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 
 

 

Approach: For increasing values of N starting from N = 1, the series will go on like 8, 64, 729, 9261, ….. for the largest N-digit perfect cube whose Nth term will be pow(ceil(cbrt(pow(10, (n))))-1, 3)
And 1, 27, 125, 1000, ….. for the smallest N-digit perfect cube whose Nth term will be pow(ceil(cbrt(pow(10, (n – 1)))), 3).
Below is the implementation of the above approach:
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
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




<?php
// PHP implementation of the approach
 
// Function to print the largest and
// the smallest n-digit perfect cube
function nDigitPerfectCubes($n)
{
 
    // Smallest n-digit perfect cube
    print(pow(ceil(pow(pow(10, ($n - 1)),1/3)), 3)." ");
 
    // Largest n-digit perfect cube
    print((int)pow(ceil(pow(pow(10, ($n)),1/3)) - 1, 3));
}
 
// Driver code
$n = 3;
nDigitPerfectCubes($n);
 
// This code is contributed by mits
?>


Javascript




<script>
 
// javascript implementation of the approach
 
// Function to print the largest and
// the smallest n-digit perfect cube
function nDigitPerfectCubes( n)
{
 
    // Smallest n-digit perfect cube
     document.write( Math.pow(Math.ceil(Math.cbrt(Math.pow(10, (n - 1)))), 3) + " ");
 
    // Largest n-digit perfect cube
     document.write( Math.pow(Math.ceil(Math.cbrt(Math.pow(10, (n)))) - 1, 3));
}
 
// Driver code
 
    let n = 3;
    nDigitPerfectCubes(n);
     
    // This code contributed by aashish1995
 
</script>


Output: 

125 729

 

Time Complexity: O(log n)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments