无法计算阶乘比20大! !怎么办呢? [英] Cannot calculate factorials bigger than 20! ! How to do so?

查看:249
本文介绍了无法计算阶乘比20大! !怎么办呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是为了计算阶乘大使用无符号长长整型格式。但是我的code在某一点失败,您可以看看吗?实际上这是一个较大的code表示指数函数的泰勒展开的一部分,但该部分是不相关的在这一点上。我将AP preciate任何建议。

感谢

 的#include<&stdio.h中GT;
#包括LT&;&math.h中GT;
//我们需要事先写一个阶乘函数,因为我们
//在分母阶乘。
//记住,阶乘的整数定义;它是
//可以使用定义非整数的阶乘
//伽玛功能,但我们会忽略这一点。
//我们首先声明的阶乘功能如下:
无符号长长因子(INT);
//很久整数格式只允许号码10 ^ 18这样的顺序
//我们应以增加我们的产品范围使用标志位。
//现在我们把它定义,
无符号长长
阶乘(INT N)
{
//这里s是其中增加一个在每个步骤和自由参数
// pro是最初的产品,并通过设置亲为0,我们还覆盖
//零阶乘的情况。
    INT S = 1;
    无符号长长亲= 1;
    如果(正℃,)
        的printf(阶乘是不是负数\\ n定义);
    其他{
    而(N> = S){
    的printf(%d个\\ N,S);
    亲* =秒;
    小号++;
    的printf(%LLU \\ n,PRO);
    }
    返回亲;
    }
}诠释的main()
{
    INT×〔12] = {1,5,10,15,20,100,-1,-5,-10,-20,-50,-100};
//这里命名为钙的数组定义存储
// x的值。
无符号长长的K =阶乘(25);
的printf(%LLU \\ n,K);//时int k;
////上部指数控制泰勒级数的精度,所以
////合适的是使它的可调参数。
// INT P = 500;
//为(K = 0; K< p; k ++);}


解决方案

这是一个unsigned long长限制为18446744073709551615,或约1.8E + 19。 20!大约是2.4E + 18,所以在范围内,但21!约5.1e + 19,超过了一个无符号的最大尺寸很长很长。

您可能会有所帮助:类型比用C长长的大++

I am using unsigned long long integer format in order to calculate big factorials. However my code fails at some point can you have a look at it? Actually it is part of a larger code for Taylor expansion of exponential function, but that part is irrelevant at this point. I will appreciate any suggestions.

Thanks

#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++);

}

解决方案

The limit on an unsigned long long is 18446744073709551615, or about 1.8e+19. 20! is about 2.4e+18, so within range, however 21! is about 5.1e+19, exceeding the maximum size of an unsigned long long.

You may find this helpful: Types bigger than long long in C++

这篇关于无法计算阶乘比20大! !怎么办呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆