Given an integer N, the task is to find the smallest N-digit number which is a perfect fourth power.
Examples:
Input: N = 2
Output: 16
Only valid numbers are 24 = 16
and 34 = 81 but 16 is the minimum.
Input: N = 3
Output: 256
44 = 256
Approach: It can be observed that for the values of N = 1, 2, 3, …, the series will go on like 1, 16, 256, 1296, 10000, 104976, 1048576, … whose Nth term will be pow(ceil( (pow(pow(10, (n – 1)), 1 / 4) ) ), 4).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// Function to return the smallest n-digit// number which is a perfect fourth powerint cal(int n){ double res = pow(ceil((pow(pow(10, (n - 1)), 1 / 4) )), 4); return (int)res;}// Driver codeint main(){ int n = 1; cout << (cal(n));}// This code is contributed by Mohit Kumar |
Java
// Java implementation of the approachclass GFG{// Function to return the smallest n-digit// number which is a perfect fourth powerstatic int cal(int n){ double res = Math.pow(Math.ceil(( Math.pow(Math.pow(10, (n - 1)), 1 / 4) )), 4); return (int)res;}// Driver codepublic static void main(String[] args){ int n = 1; System.out.println(cal(n));}}// This code is contributed by CodeMech |
Python3
# Python3 implementation of the approachfrom math import *# Function to return the smallest n-digit# number which is a perfect fourth powerdef cal(n): res = pow(ceil( (pow(pow(10, (n - 1)), 1 / 4) ) ), 4) return int(res)# Driver coden = 1print(cal(n)) |
C#
// C# implementation of the approachusing System;class GFG{// Function to return the smallest n-digit// number which is a perfect fourth powerstatic int cal(int n){ double res = Math.Pow(Math.Ceiling(( Math.Pow(Math.Pow(10, (n - 1)), 1 / 4) )), 4); return (int)res;}// Driver codepublic static void Main(){ int n = 1; Console.Write(cal(n));}}// This code is contributed// by Akanksha_Rai |
Javascript
<script>// Javascript implementation of the approach// Function to return the smallest n-digit// number which is a perfect fourth powerfunction cal(n){ var res = Math.pow(Math.ceil((Math.pow(Math.pow(10, (n - 1)), 1 / 4) )), 4); return parseInt(res);}// Driver codevar n = 1;document.write(cal(n));// This code is contributed by rutvik_56.</script> |
1
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.



