📌  相关文章
📜  在任何给定的基数中找到数字的阶乘长度

📅  最后修改于: 2021-04-23 18:06:00             🧑  作者: Mango

给定整数n和基数B ,任务是找到n!的长度在基地B中
例子:

方法:
为了解决该问题,我们使用Kamenetsky公式,该公式近似计算阶乘中的位数

f(x) =    log10( ((n/e)^n) * sqrt(2*pi*n))

n中以b为底的位数由logb(n)= log10(n)/ log10(b)给出。因此,通过使用对数的性质,可以通过以下方法获得基数b中阶乘的位数:

f(x) = ( n* log10(( n/ e)) + log10(2*pi*n)/2  ) / log10(b)

这种方法可以处理可容纳在32位整数甚至更大的整数中的大型输入!
下面的代码是上述想法的实现:

C++
// A optimised program to find the
// number of digits in a factorial in base b
#include 
using namespace std;
 
// Returns the number of digits present
// in n! in base b Since the result can be large
// long long is used as return type
long long findDigits(int n, int b)
{
    // factorial of -ve number
    // doesn't exists
    if (n < 0)
        return 0;
 
    // base case
    if (n <= 1)
        return 1;
 
    // Use Kamenetsky formula to calculate
    // the number of digits
    double x = ((n * log10(n / M_E) +
                log10(2 * M_PI * n) /
                2.0)) / (log10(b));
 
    return floor(x) + 1;
}
 
// Driver Code
int main()
{
    //calling findDigits(Number, Base)
    cout << findDigits(4, 16) << endl;
    cout << findDigits(5, 8) << endl;
    cout << findDigits(12, 16) << endl;
    cout << findDigits(19, 13) << endl;
    return 0;
}


Java
// A optimised program to find the
// number of digits in a factorial in base b
class GFG{
  
// Returns the number of digits present
// in n! in base b Since the result can be large
// long is used as return type
static long findDigits(int n, int b)
{
    // factorial of -ve number
    // doesn't exists
    if (n < 0)
        return 0;
  
    // base case
    if (n <= 1)
        return 1;
    double M_PI = 3.141592;
    double M_E = 2.7182;
     
    // Use Kamenetsky formula to calculate
    // the number of digits
    double x = ((n * Math.log10(n / M_E) +
            Math.log10(2 * M_PI * n) /
                2.0)) / (Math.log10(b));
  
    return (long) (Math.floor(x) + 1);
}
  
// Driver Code
public static void main(String[] args)
{
    //calling findDigits(Number, Base)
    System.out.print(findDigits(4, 16) +"\n");
    System.out.print(findDigits(5, 8) +"\n");
    System.out.print(findDigits(12, 16) +"\n");
    System.out.print(findDigits(19, 13) +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Python 3
from math import log10,floor
 
# A optimised program to find the
# number of digits in a factorial in base b
 
# Returns the number of digits present
# in n! in base b Since the result can be large
# long long is used as return type
def findDigits(n, b):
     
    # factorial of -ve number
    # doesn't exists
    if (n < 0):
        return 0
     
    M_PI = 3.141592
    M_E = 2.7182
 
    # base case
    if (n <= 1):
        return 1
 
    # Use Kamenetsky formula to calculate
    # the number of digits
    x = ((n * log10(n / M_E) + log10(2 * M_PI * n) / 2.0)) / (log10(b))
 
    return floor(x) + 1
 
# Driver Code
if __name__ == '__main__':
     
    #calling findDigits(Number, Base)
    print(findDigits(4, 16))
    print(findDigits(5, 8))
    print(findDigits(12, 16))
    print(findDigits(19, 13))
 
# This code is contributed by Surendra_Gangwar


C#
// A optimised C# program to find the
// number of digits in a factorial in base b
using System;
 
class GFG{
     
    // Returns the number of digits present
    // in n! in base b Since the result can be large
    // long is used as return type
    static long findDigits(int n, int b)
    {
        // factorial of -ve number
        // doesn't exists
        if (n < 0)
            return 0;
     
        // base case
        if (n <= 1)
            return 1;
        double M_PI = 3.141592;
        double M_E = 2.7182;
         
        // Use Kamenetsky formula to calculate
        // the number of digits
        double x = ((n * Math.Log10(n / M_E) +
                Math.Log10(2 * M_PI * n) /
                    2.0)) / (Math.Log10(b));
     
        return (long) (Math.Floor(x) + 1);
    }
     
    // Driver Code
    public static void Main(string[] args)
    {
        // calling findDigits(Number, Base)
        Console.WriteLine(findDigits(4, 16));
        Console.WriteLine(findDigits(5, 8));
        Console.WriteLine(findDigits(12, 16));
        Console.WriteLine(findDigits(19, 13));
    }
}
 
// This code is contributed by Yash_R


Javascript


输出:
2
3
8
16

参考: oeis.org