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++
#include <bits/stdc++.h>
using namespace std;
int countDigits( int n)
{
return (n * log10 (2) + 1);
}
int main()
{
int n = 5;
cout << countDigits(n) << endl;
return 0;
}
|
Java
import java.util.*;
class Gfg
{
static int countDigits( int n)
{
return ( int )(n * Math.log10( 2 ) + 1 );
}
public static void main(String args[])
{
int n = 5 ;
System.out.println(countDigits(n));
}
}
|
Python3
import math
def countDigits(n):
return int (n * math.log10( 2 ) + 1 );
n = 5 ;
print (countDigits(n));
|
C#
using System;
class GFG
{
static int countDigits( int n)
{
return ( int )(n * Math.Log10(2) + 1);
}
static void Main()
{
int n = 5;
Console.Write(countDigits(n));
}
}
|
PHP
<?php
function countDigits( $n )
{
return intval ( $n * log10(2) + 1);
}
$n = 5;
echo (countDigits( $n ));
?>
|
Javascript
<script>
function countDigits(n)
{
return (n * Math.log10(2) + 1);
}
let n = 5;
document.write(Math.floor(countDigits(n)));
</script>
|
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()
{
int n = 100;
double result = pow (2, n);
int num_digits = floor ( log10 (result)) + 1;
cout << "2 raised to the power of " << n << " has "
<< num_digits << " digits." ;
return 0;
}
|
Java
public class Main
{
public static void main(String[] args)
{
int n = 100 ;
double result = Math.pow( 2 , n);
int num_digits = ( int ) Math.floor(Math.log10(result)) + 1 ;
System.out.println( "2 raised to the power of " + n + " has " + num_digits + " digits." );
}
}
|
Python3
n = 100
result = 2 * * n
num_digits = len ( str (result))
print ( "2 raised to the power of" , n, "has" , num_digits, "digits." )
|
C#
using System;
public class Program
{
public static void Main()
{
int n = 100;
double result = Math.Pow(2, n);
int num_digits = ( int )Math.Floor(Math.Log10(result)) + 1;
Console.WriteLine( "2 raised to the power of " + n + " has " + num_digits + " digits." );
}
}
|
Javascript
let n = 100;
let result = Math.pow(2, n);
let num_digits = Math.floor(Math.log10(result)) + 1;
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!