Find the total Number of Digits in (N!)N
Last Updated :
26 Jul, 2022
Given a number N. The task is to find the total number of Digits in (N!)^{N} .
Examples:
Input: N = 3
Output: 3
If N=3, (3!)3=216,
So the count of digits is 3
Input: N = 4
Output: 6
Approach:
As we know,
log(a*b) = log(a) + log(b)
Consider,
X = log(N!) = log(1*2*3....... * N)
= log(1)+log(2)+........ +log(N)
Now, we know that the floor value of log base 10 increased by 1, of any number, which gives the number of digits present in that number. That is, the number of digits in a number say N will be floor(log10N) + 1. Therefore, the number of digits in (N!)^{N} will be:
floor(log((N!)^{N}))+1= floor(N*log10(N!)) + 1= floor(N*X) + 1.
Below is the implementation of the above approach:
C++
// C++ program to find the total
// Number of Digits in (N!)^N
#include <bits/stdc++.h>
using namespace std;
// Function to find the total
// Number of Digits in (N!)^N
int CountDigits(int n)
{
if (n == 1)
return 1;
double sum = 0;
// Finding X
for (int i = 2; i <= n; ++i) {
sum += (double)log(i) / (double)log(10);
}
// Calculating N*X
sum *= (double)n;
// Floor(N*X) + 1
return ceil(sum); // equivalent to floor(sum) + 1
}
// Driver code
int main()
{
int N = 5;
cout << CountDigits(N);
return 0;
}
Java
// Java program to find the total
// Number of Digits in (N!)^N
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
// Function to find the total
// Number of Digits in (N!)^N
public double CountDigits(int n)
{
if (n == 1)
return 1;
double sum = 0;
// Finding X
for (int i = 2; i <= n; ++i)
{
sum += ((double)Math.log(i) /
(double)Math.log(10));
}
// Calculating N*X
sum *= n;
// Floor(N*X) + 1
// equivalent to floor(sum) + 1
return Math.ceil(sum);
}
// Driver code
public static void main(String args[])
{
GFG g = new GFG();
int N = 5;
System.out.println(g.CountDigits(N));
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
Python3
# Python3 program to find the total
# Number of Digits in (N!)^N
import math as ma
def CountDigits(n):
if(n==1):
return 1
sum=0
# Finding X
for i in range(2,n+1):
sum+=ma.log(i,10)
# Calculating N*X
sum*=n
# Floor(N*X)+1
#equivalent to floor(sum) + 1
return ma.ceil(sum)
# Driver code
if __name__=='__main__':
N=5
print(CountDigits(N))
# This code is contributed by
# Indrajit Sinha.
C#
// C# program to find the total
// Number of Digits in (N!)^N
using System;
class GFG
{
// Function to find the total
// Number of Digits in (N!)^N
public double CountDigits(int n)
{
if (n == 1)
return 1;
double sum = 0;
// Finding X
for (int i = 2; i <= n; ++i)
{
sum += ((double)Math.Log(i) /
(double)Math.Log(10));
}
// Calculating N*X
sum *= n;
// Floor(N*X) + 1
// equivalent to floor(sum) + 1
return Math.Ceiling(sum);
}
// Driver code
public static void Main()
{
GFG g = new GFG();
int N = 5;
Console.WriteLine(g.CountDigits(N));
}
}
// This code is contributed
// by SoumikMondal
PHP
<?php
// PHP program to find the total
// Number of Digits in (N!)^N
// Function to find the total
// Number of Digits in (N!)^N
function CountDigits($n)
{
if ($n == 1)
return 1;
$sum = 0;
// Finding X
for ($i = 2; $i <= $n; ++$i)
{
$sum += log($i) / log(10);
}
// Calculating N*X
$sum *= $n;
// Floor(N*X) + 1
return ceil($sum); // equivalent to floor(sum) + 1
}
// Driver code
$N = 5;
echo CountDigits($N);
// This code is contributed by ajit
?>
JavaScript
<script>
// javascript program to find the total
// Number of Digits in (N!)^N
// Function to find the total
// Number of Digits in (N!)^N
function CountDigits(n) {
if (n == 1)
return 1;
var sum = 0;
// Finding X
for (i = 2; i <= n; ++i) {
sum += (Math.log(i) / Math.log(10));
}
// Calculating N*X
sum *= n;
// Floor(N*X) + 1
// equivalent to floor(sum) + 1
return Math.ceil(sum);
}
// Driver code
var N = 5;
document.write(CountDigits(N));
// This code contributed by aashish1995
</script>
Time Complexity: O(n) // n is the length of the array.
Auxiliary Space: O(1)
Similar Reads
Number of digits in N factorial to the power N Given a positive integer N, we have to find the total number of digits in the factorial of N raised to the power N, i.e, (N!)^N Examples: Input: 4 Output: 6 Explanations: (4!)^4 = (24)^4 = 331776. Total number of digits in 331776 is 6.Input: 5Output: 11Explanations: (5!)^5 = (120)^5 = 24883200000Tot
4 min read
Count total number of digits from 1 to n Given a number n, count the total number of digits required to write all numbers from 1 to n. Examples: Input : 13 Output : 17 Numbers from 1 to 13 are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13. So 1 - 9 require 9 digits and 10 - 13 require 8 digits. Hence 9 + 8 = 17 digits are required. Input : 4 O
8 min read
Find the frequency of a digit in a number Given a number N and a digit D. Write a program to find how many times the digit D appears in the number N. Examples : Input: N = 1122322 , D = 2 Output: 4 Input: N = 346488 , D = 9 Output: 0 The idea to solve this problem is to keep extracting digits from the number N and check the extracted digits
4 min read
n-th number whose sum of digits is ten Given an integer value n, find out the n-th positive integer whose sum is 10. Examples: Input: n = 2 Output: 28 The first number with sum of digits as 10 is 19. Second number is 28. Input: 15 Output: 154 Method 1 (Simple): We traverse through all numbers. For every number, we find the sum of digits.
8 min read
Find next greater number with same set of digits Given a number N as string, find the smallest number that has same set of digits as N and is greater than N. If N is the greatest possible number with its set of digits, then print "Not Possible".Examples: Input: N = "218765"Output: "251678"Explanation: The next number greater than 218765 with same
9 min read