Open In App

Number of digits in 2 raised to power n

Last Updated : 12 Apr, 2023
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.



Similar Reads

Larger of a^b or b^a (a raised to power b or b raised to power a)
Given two numbers [Tex]$a and b$ [/Tex], find which is greater [Tex]a^b or \, b^a [/Tex].If [Tex]a^b &gt; b^a [/Tex], print a^b is greater If [Tex]a^b &lt; b^a [/Tex], print b^a is greater If [Tex]a^b = b^a [/Tex], print Both are equalExamples: Input : 3 5 Output : a^b is greater 3^5 = 243, 5^3 = 125. Since, 243&gt;125, therefore a^b &gt; b^a. Inpu
5 min read
Count numbers from a given range that can be expressed as sum of digits raised to the power of count of digits
Given an array arr[] consisting of queries of the form {L, R}, the task for each query is to count the numbers in the range [L, R] that can be expressed as the sum of its digits raised to the power of count of digits. Examples: Input: arr[][] = {{8, 11}}Output: 2Explanation:From the given range [1, 9], the numbers that can be expressed as the sum o
10 min read
Find last five digits of a given five digit number raised to power five
Given a five-digit number N., The task is to find the last five digits of the given number raised to the power of 5 after modifying it by arranging the digits as: first digit, third digit, fifth digit, fourth digit, second digit.Examples: Input : N = 12345Output : 71232Explanation : After modification the number becomes 13542. (13542)5 is 455422043
9 min read
Find the sum of power of bit count raised to the power B
Given an integer, array A. Find the sum of set bits raised to the power A[i] for each element in A[i].Example: Input: N = 3, A[] = {1, 2, 3}Output: 10Explanation:Set bit of each array element is1 = 1 set bit, 2 = 1 set bit, 3 = 2 set bit store each set bit in b[i].Compute sum of power(b[i], i) where i is ranging from 1 to n., that is sum = power(1,
6 min read
Print last k digits of a^b (a raised to power b)
Given positive integers k, a and b we need to print last k digits of a^b ie.. pow(a, b). Input Constraint: k &lt;= 9, a &lt;= 10^6, b&lt;= 10^6 Examples: Input : a = 11, b = 3, k = 2 Output : 31 Explanation : a^b = 11^3 = 1331, hence last two digits are 31 Input : a = 10, b = 10000, k = 5 Output : 00000 Explanation : a^b = 1000..........0 (total ze
10 min read
GCD of a number raised to some power and another number
Given three numbers a, b, n. Find GCD(an, b).Examples: Input : a = 2, b = 3, n = 3 Output : 1 2^3 = 8. GCD of 8 and 3 is 1. Input : a = 2, b = 4, n = 5 Output : 4 First Approach : Brute Force approach is to first compute a^n, then compute GCD of a^n and b. C/C++ Code // CPP program to find GCD of a^n and b. #include &lt;bits/stdc++.h&gt; using name
9 min read
Minimum removals in a number to be divisible by 10 power raised to K
Given two positive integers N and K. Find the minimum number of digits that can be removed from the number N such that after removals the number is divisible by 10K or print -1 if it is impossible.Examples: Input : N = 10904025, K = 2 Output : 3 Explanation : We can remove the digits 4, 2 and 5 such that the number becomes 10900 which is divisible
12 min read
Check if a number can be expressed as x^y (x raised to power y)
Given a positive integer n, find if it can be expressed as xy where y &gt; 1 and x &gt; 0. x and y both are integers. Examples : Input: n = 8 Output: true 8 can be expressed as 23 Input: n = 49 Output: true 49 can be expressed as 72 Input: n = 48 Output: false 48 can't be expressed as xyRecommended PracticeCheck if a number can be expressed as x^yT
11 min read
Find unit digit of x raised to power y
Given two numbers x and y, find unit digit of xy. Examples : Input : x = 2, y = 1 Output : 2 Explanation 2^1 = 2 so units digit is 2. Input : x = 4, y = 2 Output : 6 Explanation 4^2 = 16 so units digit is 6. Method 1 (Simple) Compute value of xy and find its last digit. This method causes overflow for slightly larger values of x and y.Method 2 (Eff
11 min read
Find value of y mod (2 raised to power x)
Given two positive integer x and y. we have to find the value of y mod 2x. That is remainder when y is divided by 2x. Examples: Input : x = 3, y = 14 Output : 6 Explanation : 14 % 23 = 14 % 8 = 6. Input : x = 4, y = 14 Output : 14 Explanation : 14 % 24 = 14 % 16 = 14. To solve this question we can use pow() and modulo operator and can easily find t
7 min read