因子不适用于所有值 [英] Factorial does not work for all values

查看:107
本文介绍了因子不适用于所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我编辑了我的帖子,因为我有另一个问题,我的代码。我有一个问题的阶乘函数昨天,但我设法解决它感谢你的答案,这是一个可笑的错误。
现在的问题是,对于一些高于15的值,最后的结果(不是个别数字的阶乘)总是0或-1为更小的值它工作正常。有人告诉我什么错误此代码:

  #include< iostream> 
#include< time.h>

using namespace std;



int factorial(int a){
if(a == 1)
return 1;
else if(a == 0)
return 1;
else
return factorial(a-1)* a;

}

int main(){
std :: cout.setf(std :: ios_base :: fixed,std :: ios_base :: floatfield);
std :: cout.precision(5);
int n,k;
int x,y,z,w,v;


cout<<give n:;
cin>> n;


cout<<give k:;
cin>> k;

clock_t t;

t = clock();



if(n> = k& k> 1){

x = factorial(n-1)
y = factorial(k-1);
z = factorial(n-1-k);
w = factorial(n-k);
v = factorial(k);


cout<<结果是<<(x /(v * z))+(x /(y * w))<
}
else if(n == 0 || n == k)
cout<<Result is 1<< endl;
else
cout<<Result is 0<< endl;


t = clock() - t;
cout<<需要<<<<clicks(<<((float)t)/ CLOCKS_PER_SEC<<seconds)<



return 0;
}


解决方案

>

如果 int 在您的系统上是32位,则它可以表示的最大值为 2147483647 。如果您使用64位整数类型,例如,那么


$ b <



浮点会给你更多的范围,但精度的损失。

/ p>

如果你真的需要计算大的阶乘,你需要使用一些多精度库,例如 GMP ,或使用内置任意精度整数运算的语言(C不是)。


Hi everyone I edited my post because I have another problem with my code.I had a problem with the factorial function yesterday but I managed to solve it thanks to your answers , it was a ridiculous mistake. The problem now is that for some values that are higher than 15, the final results(not the factorials of individual numbers) are always 0 or -1 for the lesser values it works fine.Can someone tell me whats wrong with this code :

    #include <iostream>
    #include<time.h>

    using namespace std;



    int factorial(int a){
    if(a==1)
      return 1;
    else if(a==0)
      return 1;
    else 
      return factorial(a-1)*a;

    }

    int main(){
    std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
    std::cout.precision(5);
    int n,k;
    int x,y,z,w,v; 


    cout<<"give n : ";
    cin>>n;


     cout<<"give k : ";
     cin>>k;

     clock_t t;

        t = clock();



      if(n>=k&&k>1){

       x=factorial(n-1);
       y=factorial(k-1);
       z=factorial(n-1-k);
       w=factorial(n-k);
       v=factorial(k);


         cout<<"Result is "<<(x/(v*z))+(x/(y*w))<<endl;
       }
       else if (n==0||n==k)
         cout<<"Result is  1"<<endl;
       else
         cout<<"Result is  0"<<endl;


       t = clock() - t;
       cout<<"It took "<<t<<" clicks  ("<<((float)t)/CLOCKS_PER_SEC<<" seconds )"<<endl;    



       return 0;
       }

解决方案

Integer overflow.

If int is 32 bits on your system, the largest value it can represent is 2147483647. 13 factorial is 6227020800.

If you use a 64-bit integer type such a long long, you can go up to 20 factorial.

Floating-point will give you more range, but with a loss of precision.

If you really need to compute large factorials, you'll need to use some multi-precision library like GMP, or use a language that has built-in arbitrary-precision integer arithmetic (C doesn't).

这篇关于因子不适用于所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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