Skip to content
Related Articles

Related Articles

Smallest power of 2 consisting of N digits
  • Last Updated : 26 Sep, 2020

Given an integer N, the task is to find the smallest power of 2 which consists of N digits.

Examples:

Input: N = 3
Output: 7
Explanation:
27 = 128, which has three digits.

Input: N = 4
Output: 10
Explanation:
210 = 1024, which has four digits.

Naive Approach: A simple solution is to iterate through all the powers of 2 starting from 20 and check for each power of 2, if it contains N digits or not. Print the first power of two which contains N digits.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

10





Efficient Approach: The key observation to optimize the above approach is that the smallest power of 2 with N digits can be obtained by the equation:

\lceil (N-1)*\log_2 10 \rceil

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program of the
// above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return smallest
// power of 2 consisitng 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program of the
// above approach
class GFG{
 
  // Function to return smallest
  // power of 2 consisitng 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program of the
// above approach
using System;
class GFG{
 
// Function to return smallest
// power of 2 consisitng 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

chevron_right


Output: 

10





Time Complexity: O(1) 
Auxiliary Space: O(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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :