c ++中字符串的大数字因子 [英] factorial of big numbers with strings in c++

查看:122
本文介绍了c ++中字符串的大数字因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用字符串进行因子程序,因为我需要数字因子大于250

I am doing a factorial program with strings because i need the factorial of Numbers greater than 250

我的意图是:

string factorial(int n){
    string fact="1";
    for(int i=2; i<=n; i++){
        b=atoi(fact)*n;

    }

}

但是问题是atoi不起作用。如何将我的字符串转换为整数。

But the problem is that atoi not works. How can i convert my string in a integer.

最重要的是我想知道这种方式的程序是否适用于例如400的阶乘?

And The most important Do I want to know if the program of this way will work with the factorial of 400 for example?

推荐答案

有一个网站可以为你计算阶乘: http://www.nitrxgen.net/factorialcalc.php 。它报告:

There's a web site that will calculate factorials for you: http://www.nitrxgen.net/factorialcalc.php. It reports:


得到的阶乘250!是493位长。
中的结果还含有62个尾随零(其构成整个数目的12.58%)

The resulting factorial of 250! is 493 digits long. The result also contains 62 trailing zeroes (which constitutes to 12.58% of the whole number)

323285626090910773232081455202436847099484371767378066674794242711282374755511120948881791537102819945092850735318943292673093171280899082279103027907128192167652724018926473321804118626100683292536513367893908956993571353017504051317876007724793306540​​2339006164825552248819436572586057399222641254832982204849137721776650641276858807153128978777672951913990844377478702589172973255150283241787320658188482062478582659808848825548800000000000000000000000000000000000000000000000000000000000000

3232856260909107732320814552024368470994843717673780666747942427112823747555111209488817915371028199450928507353189432926730931712808990822791030279071281921676527240189264733218041186261006832925365133678939089569935713530175040513178760077247933065402339006164825552248819436572586057399222641254832982204849137721776650641276858807153128978777672951913990844377478702589172973255150283241787320658188482062478582659808848825548800000000000000000000000000000000000000000000000000000000000000

许多使用C ++ double 的系统只能工作到1E + 308左右;价值250!太大了,不能存储在这样的数字中。

Many systems using C++ double only work up to 1E+308 or thereabouts; the value of 250! is too large to store in such numbers.

因此,你需要使用某种多精度算术库,你自己设计的C ++ string 值,或使用其他一些广泛使用的多精度库(GNU GMP 例如)。

Consequently, you'll need to use some sort of multi-precision arithmetic library, either of your own devising using C++ string values, or using some other widely-used multi-precision library (GNU GMP for example).

这篇关于c ++中字符串的大数字因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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