Dado un número entero N , la tarea es encontrar la potencia más pequeña de 2 que consta de N dígitos.
Ejemplos:
Entrada: N = 3
Salida: 7
Explicación:
2 7 = 128, que tiene tres dígitos.Entrada: N = 4
Salida: 10
Explicación:
2 10 = 1024, que tiene cuatro dígitos.
Enfoque ingenuo : una solución simple es iterar a través de todas las potencias de 2 a partir de 2 0 y verificar para cada potencia de 2, si contiene N dígitos o no. Imprime la primera potencia de dos que contiene N dígitos.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to return smallest // power of 2 with N digits int smallestNum(int n) { int res = 1; // Iterate through all // powers of 2 for (int i = 2;; i *= 2) { int length = log10(i) + 1; if (length == n) return log(i) / log(2); } } // Driver Code int main() { int n = 4; cout << smallestNum(n); return 0; }
Java
// Java program to implement // the above approach import java.io.*; class GFG{ // Function to return smallest // power of 2 with N digits static int smallestNum(int n) { int res = 1; // Iterate through all // powers of 2 for(int i = 2;; i *= 2) { int length = (int)(Math.log10(i)) + 1; if (length == n) return (int)(Math.log(i) / Math.log(2)); } } // Driver Code public static void main (String[] args) { int n = 4; System.out.print(smallestNum(n)); } } // This code is contributed by code_hunt
Python3
# Python3 program to implement # the above approach from math import log10, log # Function to return smallest # power of 2 with N digits def smallestNum(n): res = 1 # Iterate through all # powers of 2 i = 2 while (True): length = int(log10(i) + 1) if (length == n): return int(log(i) // log(2)) i *= 2 # Driver Code n = 4 print(smallestNum(n)) # This code is contributed by SHIVAMSINGH67
C#
// C# program to implement // the above approach using System; class GFG{ // Function to return smallest // power of 2 with N digits static int smallestNum(int n) { //int res = 1; // Iterate through all // powers of 2 for(int i = 2;; i *= 2) { int length = (int)(Math.Log10(i)) + 1; if (length == n) return (int)(Math.Log(i) / Math.Log(2)); } } // Driver Code public static void Main () { int n = 4; Console.Write(smallestNum(n)); } } // This code is contributed by code_hunt
Javascript
<script> // Javascript Program to implement // the above approach // Function to return smallest // power of 2 with N digits function smallestNum(n) { res = 1; // Iterate through all // powers of 2 for (var i = 2;; i *= 2) { var length = parseInt(Math.log(i)/Math.log(10)) + 1; if (length == n) return parseInt(Math.log(i) / Math.log(2)); } } // Driver Code n = 4; document.write(smallestNum(n)); </script>
10
Enfoque eficiente : la observación clave para optimizar el enfoque anterior es que la potencia más pequeña de 2 con N dígitos se puede obtener mediante la ecuación:
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program of the // above approach #include <bits/stdc++.h> using namespace std; // Function to return smallest // power of 2 consisting of N digits int smallestNum(int n) { float power = log2(10); cout<<power; return ceil((n - 1) * power); } // Driver Code int main() { int n = 4; cout << smallestNum(n); return 0; }
Java
// Java Program of the // above approach class GFG{ // Function to return smallest // power of 2 consisting of N digits static int smallestNum(int n) { double power = log2(10); return (int) Math.ceil((n - 1) * power); } static double log2(int N) { // calculate log2 N indirectly // using log() method return (Math.log(N) / Math.log(2)); } // Driver Code public static void main(String[] args) { int n = 4; System.out.print(smallestNum(n)); } } // This code is contributed by Princi Singh
Python3
# Python3 program of the # above approach from math import log2, ceil # Function to return smallest # power of 2 with N digits def smallestNum(n): power = log2(10) print(power); return ceil((n - 1) * power) # Driver Code n = 4 print(smallestNum(n)) # This code is contributed by SHIVAMSINGH67
C#
// C# program of the // above approach using System; class GFG{ // Function to return smallest // power of 2 consisting of N digits static int smallestNum(int n) { double power = log2(10); return (int) Math.Ceiling((n - 1) * power); } static double log2(int N) { // calculate log2 N indirectly // using log() method return (Math.Log(N) / Math.Log(2)); } // Driver Code public static void Main() { int n = 4; Console.Write(smallestNum(n)); } } // This code is contributed by Chitranayal
Javascript
<script> // JavaScript program for // the above approach // Function to return smallest // power of 2 consisting of N digits function smallestNum(n) { let power = log2(10); return Math.ceil((n - 1) * power); } function log2(N) { // calculate log2 N indirectly // using log() method return (Math.log(N) / Math.log(2)); } // Driver code let n = 4; document.write(smallestNum(n)); // This code is contributed by splevel62. </script>
10
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)