给定一个整数n,求其阶乘中出现的位数,其中阶乘定义为,阶乘(n)=1*2*3*4*n和阶乘(0)=1 例如:
null
Input : n = 1Output : 11! = 1 , hence number of digits is 1Input : 5Output : 35! = 120, i.e., 3 digitsInput : 10Output : 710! = 3628800, i.e., 7 digits
一个简单的解决方案是计算n!首先,然后计算其中的位数。但是作为n的值!可能非常大,将它们存储在变量中会变得很麻烦(除非您使用的是python!)。 更好的解决方案是使用对数的有用特性来计算所需答案。
We know,log(a*b) = log(a) + log(b)Thereforelog( n! ) = log(1*2*3....... * n) = log(1) + log(2) + ........ +log(n)Now, observe that the floor value of log base 10 increased by 1, of any number, gives thenumber of digits present in that number.Hence, output would be : floor(log(n!)) + 1.
下面是一个相同实施的示例。
C++
// A C++ program to find the number of digits in // a factorial #include <bits/stdc++.h> using namespace std; // This function receives an integer n, and returns // the number of digits present in n! int findDigits( int n) { // factorial exists only for n>=0 if (n < 0) return 0; // base case if (n <= 1) return 1; // else iterate through n and calculate the // value double digits = 0; for ( int i=2; i<=n; i++) digits += log10 (i); return floor (digits) + 1; } // Driver code int main() { cout << findDigits(1) << endl; cout << findDigits(5) << endl; cout << findDigits(10) << endl; cout << findDigits(120) << endl; return 0; } |
JAVA
// Java program to find the number // of digits in a factorial import java.io.*; import java.util.*; class GFG { // returns the number of digits // present in n! static int findDigits( int n) { // factorial exists only for n>=0 if (n < 0 ) return 0 ; // base case if (n <= 1 ) return 1 ; // else iterate through n and calculate the // value double digits = 0 ; for ( int i= 2 ; i<=n; i++) digits += Math.log10(i); return ( int )(Math.floor(digits)) + 1 ; } // Driver code public static void main (String[] args) { System.out.println(findDigits( 1 )); System.out.println(findDigits( 5 )); System.out.println(findDigits( 10 )); System.out.println(findDigits( 120 )); } } // This code is contributed by Pramod Kumar |
Python3
# Python3 program to find the # number of digits in a factorial import math # This function receives an integer # n, and returns the number of # digits present in n! def findDigits(n): # factorial exists only for n>=0 if (n < 0 ): return 0 ; # base case if (n < = 1 ): return 1 ; # else iterate through n and # calculate the value digits = 0 ; for i in range ( 2 , n + 1 ): digits + = math.log10(i); return math.floor(digits) + 1 ; # Driver code print (findDigits( 1 )); print (findDigits( 5 )); print (findDigits( 10 )); print (findDigits( 120 )); # This code is contributed by mits |
C#
// A C++ program to find the number // of digits in a factorial using System; class GFG { // This function receives an integer // n, and returns the number of // digits present in n! static int findDigits( int n) { // factorial exists only for n>=0 if (n < 0) return 0; // base case if (n <= 1) return 1; // else iterate through n and // calculate the value double digits = 0; for ( int i = 2; i <= n; i++) digits += Math.Log10(i); return ( int )Math.Floor(digits) + 1; } // Driver code public static void Main() { Console.Write(findDigits(1) + "" ); Console.Write(findDigits(5) + "" ); Console.Write(findDigits(10) + "" ); Console.Write(findDigits(120) + "" ); } } // This code is contributed by // Smitha Dinesh Semwal |
PHP
<?php // PHP program to find // the number of digits // in a factorial // This function receives // an integer n, and returns // the number of digits present in n! function findDigits( $n ) { // factorial exists only for n>=0 if ( $n < 0) return 0; // base case if ( $n <= 1) return 1; // else iterate through n and // calculate the value $digits = 0; for ( $i = 2; $i <= $n ; $i ++) $digits += log10( $i ); return floor ( $digits ) + 1; } // Driver code echo findDigits(1), "" ; echo findDigits(5), "" ; echo findDigits(10), "" ; echo findDigits(120), "" ; // This code is contributed by Ajit. ?> |
Javascript
<script> // A Javascript program to find the number of digits in // a factorial // This function receives an integer n, and returns // the number of digits present in n! function findDigits(n) { // factorial exists only for n>=0 if (n < 0) return 0; // base case if (n <= 1) return 1; // else iterate through n and calculate the // value let digits = 0; for (let i=2; i<=n; i++) digits += Math.log10(i); return Math.floor(digits) + 1; } // Driver code document.write(findDigits(1) + "<br>" ); document.write(findDigits(5) + "<br>" ); document.write(findDigits(10) + "<br>" ); document.write(findDigits(120) + "<br>" ); //This code is contributed by Mayank Tyagi </script> |
输出:
137199
在下一集中,我们将看到如何进一步优化我们的方法,并降低同一程序的时间复杂度。 本文由 阿什图什·库马尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END