使用Rmpfr/gmp库的x> 170的阶乘(x) [英] Factorial(x) for x>170 using Rmpfr/gmp library

查看:131
本文介绍了使用Rmpfr/gmp库的x> 170的阶乘(x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解决的问题是以下函数的无穷大:

The problem that I would like to solve is the infinite sum over the following function:

对于总和,我使用FTOL确定标准.在z变大之前,整个术语不会产生任何问题.我希望z的最大值在220附近.如您所见,第一个项的最大值在Factorial(221)附近,因此必须在Factorial(500)附近,直到达到确定标准为止.发现此问题后,我不想更改整个代码(因为它只是一小部分),而是尝试使用library('Rmpfr')library('gmp').问题是我没有得到想要的东西.虽然乘法正常工作,但对于更高的值,减法将失败:

For the sum I use an FTOL determination criterion. This whole term doesn't create any problems until z becomes very large. I expect the maximum value of z around 220. As you can see the first term has its max around Factorial(221) and therefore has to go around Factorial(500) until the determination criterion has been reached. After spotting this problem I didn't want to change the whole code (as it is only one small part) and tried to use library('Rmpfr') and library('gmp'). The problem is that I do not get what I want to. While multiplication normally works, subtraction fails for higher values:

这有效

> factorialZ(22)-factorial(22)
Big Integer ('bigz') :
[1] 0

但这失败了:

> factorialZ(50)-factorial(50)
Big Integer ('bigz') :
[1] 359073645150499628823711419759505502520867983196160

我尝试过的另一种方法:

another way I tried:

> gamma(as(10,"mpfr"))-factorial(9)
1 'mpfr' number of precision  128   bits 
[1] 0

> gamma(as(40,"mpfr"))-factorial(39)
1 'mpfr' number of precision  128   bits 
[1] 1770811808798664813196481658880

必须有些我不太了解的东西.有人可以为这个问题提供更好的解决方案吗?有人可以帮助我解决上述问题吗?

There has to be something that I don't really understand. Does someone have a even better solution for the problem or can someone help me out with the issue above?

推荐答案

我认为您错误地理解了factorialZ(x)-factorial(x)中的优先级.第二项factorial(x)之前计算得出,并转换为bigz与第一项组合.

I think you incorrectly understand the priorities in factorialZ(x)-factorial(x) . The second term, factorial(x) is calculated before it's converted to a bigz to be combined with the first term.

必须使用兼容bigz的函数创建2 ^ 64(或其他值,取决于您的计算机)范围之外的任何整数.

You must create any integer outside the 2^64 (or whatever, depending on your machine) range using a bigz - compatible function.

这篇关于使用Rmpfr/gmp库的x> 170的阶乘(x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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