Open In App

Number of digits in 2 raised to power n

Improve
Improve
Like Article
Like
Save
Share
Report

Let n be any power raised to base 2 i.e 2n. We are given the number n and our task is to find out the number of digits contained in the number 2n.
Examples: 

Input : n = 5
Output : 2
Explanation : 2n = 32, which has only
2 digits.

Input : n = 10
Output : 4
Explanation : 2n = 1024, which has only
4 digits.

We can write 2n using logarithms as:  

2n = 10nlog102

Now suppose, x = nlog102, 
Therefore, 2n = 10x
Also, we all know that the number, 10n will have (n+1) digits. Therefore, 10x will have (x+1) digits.
Or, we can say that 2n will have (x+1) digits as 2n = 10x.
Therefore, number of digits in 2n = (nlog102) + 1
Below is the implementation of above idea:  

C++




// CPP program to find number of digits
// in 2^n
#include <bits/stdc++.h>
using namespace std;
 
// Function to find number of digits
// in 2^n
int countDigits(int n)
{
    return (n * log10(2) + 1);
}
 
// Driver code
int main()
{
    int n = 5;
    cout << countDigits(n) << endl;
    return 0;
}


Java




// Java program to find number
// of digits in 2^n
import java.util.*;
 
class Gfg
{
    // Function to find number of digits
    // in 2^n
    static int countDigits(int n)
    {
        return (int)(n * Math.log10(2) + 1);
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int n = 5;
        System.out.println(countDigits(n));
    }
}
 
// This code is contributed by Niraj_Pandey.


Python3




# Python3 program to find
# number of digits in 2^n
import math
 
# Function to find number
# of digits in 2^n
def countDigits(n):
    return int(n * math.log10(2) + 1);
 
# Driver code
n = 5;
print(countDigits(n));
 
# This code is contributed
# by mits


C#




// C# program to find number
// of digits in 2^n
using System;
 
class GFG
{
    // Function to find
    // number of digits in 2^n
    static int countDigits(int n)
    {
        return (int)(n * Math.Log10(2) + 1);
    }
     
    // Driver code
    static void Main()
    {
        int n = 5;
        Console.Write(countDigits(n));
    }
}
// This code is contributed by
// Manish Shaw(manishshaw1)


PHP




<?php
// PHP program to find
// number of digits in 2^n
 
// Function to find number
// of digits in 2^n
function countDigits($n)
{
    return intval($n * log10(2) + 1);
}
 
// Driver code
$n = 5;
echo (countDigits($n));
 
// This code is contributed by
// Manish Shaw(manishshaw1)
?>


Javascript




<script>
 
// JavaScript program to find number
// of digits in 2^n
 
    // Function to find number of digits
    // in 2^n
    function countDigits(n)
    {
        return (n * Math.log10(2) + 1);
    }
 
// Driver code
 
        let n = 5;
        document.write(Math.floor(countDigits(n)));
              
             // This code is contributed by souravghosh0416.
</script>


Output

2

Time Complexity: O(1)
Auxiliary Space: O(1)

Python program that calculates the number of digits in 2 raised to the power of n:

This program first defines the value of n, and then calculates 2 raised to the power of n using the ** operator. The len() function is then used to count the number of digits in the result by converting it to a string. The result is then printed to the console.

C++




#include <cmath>
#include <iostream>
using namespace std;
 
int main()
{
    // Define the value of n
    int n = 100;
    // Calculate 2^n
    double result = pow(2, n);
 
    // Count the number of digits in the result
    int num_digits = floor(log10(result)) + 1;
 
    // Print the number of digits
    cout << "2 raised to the power of " << n << " has "
         << num_digits << " digits.";
 
    return 0;
}


Java




public class Main
{
    public static void main(String[] args)
    {
       
        // Define the value of n
        int n = 100;
 
        // Calculate 2^n
        double result = Math.pow(2, n);
 
        // Count the number of digits in the result
        int num_digits = (int) Math.floor(Math.log10(result)) + 1;
 
        // Print the number of digits
        System.out.println("2 raised to the power of " + n + " has " + num_digits + " digits.");
    }
}


Python3




# Define the value of n
n = 100
 
# Calculate 2^n
result = 2 ** n
 
# Count the number of digits in the result
num_digits = len(str(result))
 
# Print the number of digits
print("2 raised to the power of", n, "has", num_digits, "digits.")


C#




using System;
 
public class Program
{
    public static void Main()
    {
        // Define the value of n
        int n = 100;
 
        // Calculate 2^n
        double result = Math.Pow(2, n);
 
        // Count the number of digits in the result
        int num_digits = (int)Math.Floor(Math.Log10(result)) + 1;
 
        // Print the number of digits
        Console.WriteLine("2 raised to the power of " + n + " has " + num_digits + " digits.");
    }
}
 
// This code in contributed by shiv1o43g


Javascript




// Define the value of n
let n = 100;
 
// Calculate 2^n
let result = Math.pow(2, n);
 
// Count the number of digits in the result
let num_digits = Math.floor(Math.log10(result)) + 1;
 
// Print the number of digits
console.log(`2 raised to the power of ${n} has ${num_digits} digits.`);


Output

2 raised to the power of 100 has 31 digits.

The time complexity of this program is O(log n), as the number of digits in the result is proportional to the logarithm of the value of 2 raised to the power of n. The len() function has a time complexity of O(1), as it simply returns the length of the string representation of the result.

The auxiliary space of this program is also O(log n), as the result of 2 raised to the power of n can have up to log(n) digits, and the str() function creates a string representation of the result.



Last Updated : 12 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads