13

我正在使用无符号长整数格式来计算大阶乘。但是我的代码在某些时候失败了你能看看吗?实际上,它是指数函数泰勒展开的较大代码的一部分,但此时该部分无关紧要。我将不胜感激任何建议。

谢谢

#include <stdio.h>
#include <math.h>
//We need to write a factorial function beforehand, since we
//have factorial in the denominators.
//Remembering that factorials are defined for integers; it is
//possible to define factorials of non-integer numbers using
//Gamma Function but we will omit that.
//We first declare the factorial function as follows:
unsigned long long factorial (int);
//Long long integer format only allows numbers in the order of 10^18 so 
//we shall use the sign bit in order to increase our range.
//Now we define it,
unsigned long long
factorial(int n)
{
//Here s is the free parameter which is increased by one in each step and
//pro is the initial product and by setting pro to be 0 we also cover the
//case of zero factorial.
    int s = 1;
    unsigned long long pro = 1;
    if (n < 0)
        printf("Factorial is not defined for a negative number \n");
    else {
    while (n >= s) { 
    printf("%d \n", s);
    pro *= s;
    s++;
    printf("%llu \n", pro);
    }
    return pro;
    }
}

int main ()
{
    int x[12] = { 1, 5, 10, 15, 20, 100, -1, -5, -10, -20, -50, -100};
//Here an array named "calc" is defined to store 
//the values of x.
unsigned long long  k = factorial(25);
printf("%llu \n", k);

//int k;
////The upper index controls the accuracy of the Taylor Series, so
////it is suitable to make it an adjustable parameter. 
//int p = 500;
//for ( k = 0; k < p; k++);

}
4

7 回答 7

15

unsigned long long 的限制是 18446744073709551615,或大约 1.8e+19。20!大约是 2.4e+18,所以在范围内,但是 21!大约是 5.1e+19,超过了 unsigned long long 的最大大小。

您可能会发现这很有帮助:C++ 中是否存在比 long long int 更大的类型?

于 2013-10-07T17:05:06.143 回答
7

你溢出了你的整数类型。对您来说,这可能unsigned long long是 64 位长。

  • 20!是0x21c3_677c_82b4_0000哪个适合。
  • 21!是0x2_c507_7d36_b8c4_0000哪个不适合。

您可以查看诸如GMP之类的库,它们支持任意大的整数。


扩展 GMP 注释。下面是一些使用 GMP 计算阶乘的代码:

void factorial(unsigned long long n, mpz_t result) {
    mpz_set_ui(result, 1);

    while (n > 1) {
        mpz_mul_ui(result, result, n);
        n = n-1;
    }
}

int main() {
    mpz_t fact;
    mpz_init(fact);

    factorial(100, fact);

    char *as_str = mpz_get_str(NULL, 16, fact);
    printf("%s\n", as_str);

    mpz_clear(fact);
    free(as_str);
}

这将计算factorial(100),并导致:

0x1b30964ec395dc24069528d54bbda40d16e966ef9a70eb21b5b2943a321cdf10391745570cca9420c6ecb3b72ed2ee8b02ea2735c61a000000000000000000000000

只是为了好玩,这里是 C++ 版本。构造函数、析构函数和运算符重载往往使这些东西的 C++ 版本看起来更干净一些。结果和以前一样。

#include <gmpxx.h>
#include <iostream>

int main() {
    mpz_class fact = 1;

    for (int i=2; i<=100; ++i)
        fact *= i;

    std::cout << "0x" << fact.get_str(16) << "\n";
}
于 2013-10-07T17:03:47.917 回答
5

的范围unsigned long long通常是02^64 - 1 ( 18,446,744,073,709,551,615)。21!超出这个范围。

于 2013-10-07T17:04:13.617 回答
4

的确:

2^64 = 18446744073709551616
21!  = 51090942171709440000
20!  =  2432902008176640000

顺便说一句,要计算系列的结果(例如,泰勒),您不应该单独计算每个项;这肯定会给您带来诸如此类的问题。相反,尝试通过重用前一项来计算每个项。

例如,泰勒级数cos需要以下项的总和:

(-1)^i * (x^(2*i)) / (2i)!

很容易看出,每一项都可以很容易地从前一项计算出来:

newterm = - oldterm * x^2 / ((2i+1)*(2i+2))

所以,我相信你不需要为你想做的事情计算大的阶乘。另一方面,如果需要,您将不得不使用大数字库,例如gmp.

于 2013-10-07T17:04:34.957 回答
2

factorial(25) should give the result 18,446,744,073,709,551,615 which is larger than the range of unsigned long long Data Type Ranges

于 2013-10-07T17:05:26.163 回答
2

A long long is only so big, and thus can only represent numbers so big. If you need an exact representation of bigger integers, you'll need to use something else (some 3-rd party library or some datatype you make yourself); if you don't need it to be exact, then you could use double's instead.

于 2013-10-07T17:06:16.813 回答
0

我写的一个简单的算法。但它是用 Java 编写的。你可以在大约 15 分钟内计算出 1000 的阶乘。

该算法适用于我们在小学学习的基本公式。

/* FOR BEST RESULT DON'T CHANGE THE CODE UNTIL YOU KNOW WHAT YOU'RE DOING */
    public String factorial(int number){
        if(number == 0) return "1";
        String result = "1";
        for(int i = 0; i < number; i++){
            result = *longNumberMultiplyingAlgorithm*(result, "" + (i + 1));
        }
        return result;  
    }

    public String longNumberMultiplyingAlgorithm(String number1, String number2){
            int maxLength = Math.max(number1.length(), number2.length());
            int a = 0;
            String[] numbers = new String[maxLength];

            if(number2.length() > number1.length()){
                String t = number1;
                number1 = number2;
                number2 = t;
            }       

            for(int i = 0; i < number1.length(); i++){
                numbers[i] = "";
                a = 0;
                for(int j = 0; j < number2.length(); j++){
                    int result = Integer.parseInt(String.valueOf(number1.charAt(number1.length() - i - 1))) * Integer.parseInt(String.valueOf(number2.charAt(number2.length() - j - 1)));
                    if(result + a < 10){
                        numbers[i] = (result + a) + "" + numbers[i];
                        a = 0;
                    }else{
                        result += a;
                        a = (int)((result + 0.0) / 10);
                        result -= a * 10;
                        numbers[i] = result + "" + numbers[i];
                    }
                }
                if(a != 0){
                    numbers[i] = a + "" + numbers[i];
                }
                for(int k = 0; k < i; k++){
                    numbers[i] += "0";
                }
            }
            return longNumberAdditionAlgorithm(numbers);
        }

    private String longNumberAdditionAlgorithm(String[] numbers) {
            String final_number = "0";
            for(int l = 0; l < numbers.length; l++){
                int maxLength = Math.max(final_number.length(), numbers[l].length());
                String number = "";
                int[] n = new int[maxLength];
                int a = 0;
                for(int i = 0; i < n.length; i++){
                    int result = 0;
                    if(i >= final_number.length()){
                        result = Integer.parseInt(String.valueOf(numbers[l].charAt(numbers[l].length() - i - 1)));
                    }else
                    if(i >= numbers[l].length()){
                        result = Integer.parseInt(String.valueOf(final_number.charAt(final_number.length() - i - 1)));
                    }else{
                        result = Integer.parseInt(String.valueOf(final_number.charAt(final_number.length() - i - 1))) + Integer.parseInt(String.valueOf(numbers[l].charAt(numbers[l].length() - i - 1)));
                    }
                    if(result + a < 10){
                        number = (result + a) + "" + number;
                        a = 0;
                    }else{
                        result -= 10;
                        number = (result + a) + "" + number;
                        a = 1;
                    }
                }
                if(a == 1){
                    number = a + "" + number;
                }
                final_number = number;
            }
            return final_number;
        }
于 2014-07-09T18:33:39.593 回答