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 ?> |
125 729
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Find smallest perfect square number A such that N + A is also a perfect square number
- Perfect cubes in a range
- Sum of all Perfect Cubes lying in the range [L, R] for Q queries
- Number of perfect cubes between two given numbers
- Check if a number can be represented as sum of two consecutive perfect cubes
- Smallest and Largest N-digit perfect squares
- Count numbers upto N which are both perfect square and perfect cube
- Check if a number is a perfect square having all its digits as a perfect square
- Sum of cubes of even and odd natural numbers
- Program to count number of distinct Squares and Cubes upto N
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Smallest perfect Cube divisible by all elements of an array
- Smallest perfect cube in an array
- Smallest N digit number which is a perfect fourth power
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Number of times the largest perfect square number can be subtracted from N
- Largest number that is not a perfect square
- Largest factor of a given number which is a perfect square
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

