我正在编写一个脚本,为插入的数字提供阶乘,但我在乘法时遇到了一些问题.
注意:因子由下式给出:9!= 9*8*7*6*5*4*3*2*1
这是我的代码:
#!/bin/bash
echo "Insert an Integer"
read input
if ! [[ "$input" =~ ^[0-9]+$ ]] ; then
exec >&2; echo "Error: You didn't enter an integer"; exit 1
fi
function factorial
{
while [ "$input" != 1 ];
do
result=$(($result * $input))
input=$(($input-1))
done
}
factorial
echo "The Factorial of " $input "is" $result
Run Code Online (Sandbox Code Playgroud)
它不断给我不同的乘法技术的各种错误:/
目前输出是:
joaomartinsrei@joaomartinsrei ~/Área de Trabalho/Shell $ ./factorial.sh
Insert an Integer
3
./factorial.sh: line 15: * 3: syntax error: operand expected (error token …Run Code Online (Sandbox Code Playgroud) 以下不编译:
Func<int, int> fac = n => (n <= 1) ? 1 : n * fac(n - 1);
Run Code Online (Sandbox Code Playgroud)
在访问之前,可能不会初始化局部变量'fac'
你怎么能用lambdas做一个递归函数?
[更新]
这里还有两个我觉得有趣的链接:
我试图在python中计算泊松分布如下:
p = math.pow(3,idx)
depart = math.exp(-3) * p
depart = depart / math.factorial(idx)
Run Code Online (Sandbox Code Playgroud)
idx范围从0
但是我得到了 OverflowError: long int too large to convert to float
我试图将转换转换为float但没有结果.
最近我在一次采访中被要求描述一种计算任意大数的阶乘的方法; 一种方法,我们获得答案的所有数字.
我搜索了不同的地方,并在几个论坛上询问.但我想知道是否有任何方法可以在不使用像GMP这样的库的情况下实现这一目标.
谢谢.
我遇到了以下计算大型因子(数字大到100)的程序..谁能解释一下这个算法中使用的基本思想?我只需要知道在计算阶乘时实现的数学.
#include <cmath>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
unsigned int d;
unsigned char *a;
unsigned int j, n, q, z, t;
int i,arr[101],f;
double p;
cin>>n;
p = 0.0;
for(j = 2; j <= n; j++)
p += log10(j);
d = (int)p + 1;
a = new unsigned char[d];
for (i = 1; i < d; i++)
a[i] = 0; //initialize
a[0] = 1;
p = 0.0;
for (j = 2; j <= n; j++) …Run Code Online (Sandbox Code Playgroud) 在RI我发现了一些我无法解释的奇怪行为,我希望有人在这里.我相信100的价值!这是一个很大的数字.
控制台的几行显示预期的行为......
>factorial( 10 )
[1] 3628800
>prod( 1:10 )
[1] 3628800
> prod( as.double(1:10) )
[1] 3628800
> cumprod( 1:10 )
[1] 1 2 6 24 120 720 5040 40320 362880 3628800
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试100!我明白了(注意结果数字在14位左右开始有所不同):
> options(scipen=200) #set so the whole number shows in the output
> factorial(100)
[1] 93326215443942248650123855988187884417589065162466533279019703073787172439798159584162769794613566466294295348586598751018383869128892469242002299597101203456
> prod(1:100)
[1] 93326215443944102188325606108575267240944254854960571509166910400407995064242937148632694030450512898042989296944474898258737204311236641477561877016501813248
> prod( as.double(1:100) )
[1] 93326215443944150965646704795953882578400970373184098831012889540582227238570431295066113089288327277825849664006524270554535976289719382852181865895959724032
> all.equal( prod(1:100) , factorial(100) , prod( as.double(1:100) ) )
[1] TRUE
Run Code Online (Sandbox Code Playgroud)
如果我对一个设置为'已知'数字100的变量进行一些测试!然后我看到以下内容:
# This is (as far as …Run Code Online (Sandbox Code Playgroud) 我遇到的一个编程问题涉及计算大数(最多10 ^ 5的数字)的阶乘.我见过一个简单的Haskell代码,就像这样
factorial :: (Eq x, Num x) => x -> x
factorial 0 = 1
factorial a = a * factorial (a - 1)
Run Code Online (Sandbox Code Playgroud)
即使没有代码中涉及的任何缓存,它也会隐式处理大量数字并以某种方式运行得更快.
当我尝试使用Java解决问题时,我不得不使用BigInteger来保存大数字并使用因子的迭代版本
public static BigInteger factorialIterative(int n)
{
if(n == 0 || n == 1) return BigInteger.valueOf(1);
BigInteger f = BigInteger.valueOf(1);
for(int i = 1 ; i <= n ;i++)
f = f.multiply(BigInteger.valueOf(i));
return f;
}
Run Code Online (Sandbox Code Playgroud)
上述代码超出了程序执行的设定时间限制.我也尝试了factorial的缓存递归版本
public static BigInteger factorial(int n)
{
if(cache[n] != null)
return cache[n];
else if(n == 0)
return new …Run Code Online (Sandbox Code Playgroud) 今天在课堂上我的老师在黑板上写下了这个递归因子算法:
int factorial(int n) {
if (n == 1)
return 1;
else
return n * factorial(n-1);
}
Run Code Online (Sandbox Code Playgroud)
她说它有成本T(n-1) + 1.
然后用迭代法她说T(n-1) = T(n-2) + 2 = T(n-3) + 3 ... T(n-j) + j,所以算法停止的时候n - j = 1,所以j = n - 1.
之后,她取代j的T(n-j) + j,并获得了T(1) + n-1.
她直接说那个n-1 = 2 (log 2 n-1),所以算法的成本是指数的.
我真的失去了最后两步.可以请有人向我解释一下吗?