存储号码大于20! (阶乘) [英] Storing a number greater than 20! (factorial)

查看:108
本文介绍了存储号码大于20! (阶乘)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图寻找,直到数 100 <!/ code>(阶乘),但后 20 <!/ code>它给出了一个错误作为值过大,无法处理。我怎么能存储这样一个数字?


解决方案

20阶乘是19位长。
100阶乘是158位长,占用了500位!它实际上是:

<$p$p><$c$c>933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000
00000000

标准类型都不会为你在这里工作,你必须做的就是实现自己的日常处理这种大量涌现。

例如,你将需要接受两个32位值相乘,可能会导致在64位的结果。

这是在8位处理器的天计算大量coommon做法。
它被称为任意precision arithemtic。

这里的一个链接来描述这​​是怎么做的。无可否认它是组装,但它仍然在这里也是如此。你需要考虑到您的系统上 INT 的默认大小。

I am trying to find the numbers till 100! (factorial) but after 20! it gives an error as the value is too large to handle. How can I store such a number?

解决方案

20 factorial is 19 digits long. 100 factorial is 158 digits long, taking up 500 bits! It's actually:

933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000
00000000

None of the standard types will work for you here, what you'll have to do is implement your own routine for handling such large numbers.

For example you will need to accept that multiplying two 32 bit values together may result in a 64 bit result.

This was coommon practice for calculating large numbers in the days of 8 bit processors. Its referred to as arbitrary precision arithemtic.

Here's a link to describe how this was done. Admittedly it's for assembler, but it still holds true here. You'll need to take into account the default sizes of int on your system.

这篇关于存储号码大于20! (阶乘)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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