最近我在一次采访中被要求描述一种计算任意大数的阶乘的方法; 一种方法,我们获得答案的所有数字.
我搜索了不同的地方,并在几个论坛上询问.但我想知道是否有任何方法可以在不使用像GMP这样的库的情况下实现这一目标.
谢谢.
我正在编写一种编程语言,今天我得到了可以编译阶乘函数(递归)的点,但是由于整数的最大值,我能得到的最大值是阶乘(12).什么是处理任意最大大小的整数的技术.该语言目前通过将代码转换为C++来工作.
我使用无符号长整数格式来计算大因子.但是我的代码在某些时候失败了你能看一下吗?实际上它是指数函数的泰勒展开的更大代码的一部分,但是这一部分在这一点上是无关紧要的.我将不胜感激任何建议.
谢谢
#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 …Run Code Online (Sandbox Code Playgroud) 如何编写c ++程序来计算大因子.
例如,如果我想计算(100!)/(99!),我们知道答案是100,但如果我分别计算分子和分母的阶乘,那么这两个数字都是巨大的.