我尝试使用for-loop和double数据类型以典型方式找到大数的阶乘,例如8785856.
但结果显示无穷大,可能是因为它超出了极限.
所以请指导我找到一个非常大的阶乘的方法.
我的代码:
class abc
{
public static void main (String[]args)
{
double fact=1;
for(int i=1;i<=8785856;i++)
{
fact=fact*i;
}
System.out.println(fact);
}
}
Run Code Online (Sandbox Code Playgroud)
输出: -
Infinity
Run Code Online (Sandbox Code Playgroud)
我是Java的新手,但他已经学会了一些IO处理的概念.
为什么下面的程序将 100 作为输入的阶乘打印为 0。如果 getFact 函数返回类型为 ,则可以计算相同的阶乘long double,但为了获得数字总和,我无法在 long double 上应用mod (%)运算符。
注意:unsigned long long和的大小long double在我的机器上相同。请建议输入为 100 什么类型的数据会给出正确的输出。
#include <iostream>
#include <stdlib.h>
unsigned long long int getFactorial(int);
unsigned long long int getSum(unsigned long long int);
int main()
{
unsigned long long int fact = 1;
unsigned long long int digsum = 0;
std::cout << "Enter a number to find fact := ";
int num;
std::cin >> num;
fact = getFactorial(num);
std::cout << …Run Code Online (Sandbox Code Playgroud) 我需要计算高达100左右的阶乘数!为了确定一系列硬币翻转式数据是否是随机的,根据贝叶斯概率的维基百科条目. 正如你在那里看到的那样,必要的公式涉及3个因子计算(但是,有趣的是,这些因子计算中的两个是沿着到第三个计算的方式计算的).
我在这里看到了这个问题,但我认为这个整数很快就会被吹灭.我也可以创建一个更加智能的因子计算功能(即,如果我有11!/(7!3!),根据wiki示例,我可以去(11*10*9*8)/ 3!),但这对我来说过早优化,在某种意义上我希望它能够工作,但我并不关心速度(还).
那么,为了获得这个概率,我可以调用什么样的C#库来计算阶乘?我对可以进入阶乘计算的所有可怕性感兴趣,我只想以一种我可以操纵它的方式得到结果.在Math命名空间中似乎没有因子函数,因此问题.
每当我试图获得171的阶乘,我得到INF.170工作正常.是否有可能在脚本中获得171+的阶乘?怎么样?我的功能:
function factorial($n) {
if ($n == 0) return 1;
return $n * factorial($n - 1);
}
Run Code Online (Sandbox Code Playgroud) 我编码了3个因子算法:
trampoline()方法,并按照我的预期正常工作.def factorial
factorial = { BigInteger n ->
if (n == 1) return 1
n * factorial(n - 1)
}
factorial(1000) // Stack Overflow
factorial = { Integer n, BigInteger acc = 1 ->
if (n == 1) return acc
factorial(n - 1, n * acc)
}
factorial(1000) // Stack Overflow, why???
factorial = { Integer n, BigInteger acc = 1 ->
if (n == 1) return acc
factorial.trampoline(n …Run Code Online (Sandbox Code Playgroud) recursion groovy tail-recursion factorial tail-call-optimization
这是我的阶乘方法:
public static long factorial(int num1) {
if (num1 <= 1)
return 1;
else
return num1 * factorial(num1 - 1);
}
Run Code Online (Sandbox Code Playgroud)
这就是所谓的递归因子方法:
for (i = 0; i <= 25; i++)
System.out.printf ("%d != %,d\n", i, factorial (i));
Run Code Online (Sandbox Code Playgroud)
到目前为止一切顺利,输出看起来似乎是正确的,但是一些因子是负的而不是正的:
OUTPUT:
0 != 1
1 != 1
2 != 2
3 != 6
4 != 24
5 != 120
6 != 720
7 != 5,040
8 != 40,320
9 != 362,880
10 != 3,628,800
11 != 39,916,800
12 != 479,001,600
13 != …Run Code Online (Sandbox Code Playgroud) 我是Python 新手,目前正在阅读Python 3的绝对初学者和面对以下问题.
我想用程序计算阶乘.
而代码是这样的:
N = input("Please input factorial you would like to calculate: ")
ans = 1
for i in range(1,N+1,1):
ans = ans*i
print(ans)
Run Code Online (Sandbox Code Playgroud)
而我想添加一项功能来检查输入数字N是否为非负数.喜欢:
if N != int(N) and N < 0:
Run Code Online (Sandbox Code Playgroud)
我希望用户再次输入N,如果它不是非负数.
谢谢你的温柔帮助.
我想知道如何逐元素计算矩阵的阶乘。例如,
import numpy as np
mat = np.array([[1,2,3],[2,3,4]])
np.the_function_i_want(mat)
Run Code Online (Sandbox Code Playgroud)
会给一个矩阵mat2,使得mat2[i,j] = mat[i,j]!。我已经尝试过类似
np.fromfunction(lambda i,j: np.math.factorial(mat[i,j]))
Run Code Online (Sandbox Code Playgroud)
但它会将整个矩阵作为参数传递np.math.factorial。我也尝试过使用,scipy.vectorize但是对于大于10x10的矩阵,我得到一个错误。这是我写的代码:
import scipy as sp
javi = sp.fromfunction(lambda i,j: i+j, (15,15))
fact = sp.vectorize(sp.math.factorial)
fact(javi)
OverflowError: Python int too large to convert to C long
Run Code Online (Sandbox Code Playgroud)
这样的整数将大于2e9,所以我不明白这意味着什么。
我想编写一个使用并行计算(Open MP 库)计算整数阶乘的程序。
显然,下面的程序存在竞争条件。
// Each loop iteration writes a value that a different iteration reads.
#pragma omp parallel for
for (i=2; i < 10; i++)
{
factorial[i] = i * factorial[i-1];
}
Run Code Online (Sandbox Code Playgroud)
我在某处读到 pow 和阶乘计算绝不能并行完成。那么,这是真的,还是可以修改上述程序(在 C 中,使用 OPenMP 库)以并行计算阶乘?
我想知道是否可以使用Control.Arrow.loop实现阶乘。
loop :: ArrowLoop a => a (b, d) (c, d) -> a b c
一个显而易见的想法是实现某种终止分支(该分支对(类型c)的第一个元素不依赖于该对(类型d)的第二个元素的分支)。在我看来,这是无法完成的,因为我们无法d在第一次迭代期间将任何布尔函数应用于该对(type )的第二个元素,因为它将导致无限递归,因此只给我们提供了参数(类型b),但任何布尔函数的结果都不会因迭代而有所不同(参数不变),因此,它要么立即终止,要么根本不会终止。我的另一个想法是创建无穷无尽的析因流,但这似乎也不是真实的,因为再次不能更改参数。因此,我有3个问题:
Control.Arrow.loop?