标签: factorial

变量乘法

我正在编写一个脚本,为插入的数字提供阶乘,但我在乘法时遇到了一些问题.

注意:因子由下式给出: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)

bash multiplication factorial

32
推荐指数
1
解决办法
8万
查看次数

C#:使用Lambdas的递归函数

以下不编译:

Func<int, int> fac = n => (n <= 1) ? 1 : n * fac(n - 1);
Run Code Online (Sandbox Code Playgroud)

在访问之前,可能不会初始化局部变量'fac'

你怎么能用lambdas做一个递归函数?

[更新]

这里还有两个我觉得有趣的链接:

  1. Eric Lippert的"为什么递归lambda导致明确的赋值错误?"
  2. C#中的匿名递归

c# recursion lambda factorial

27
推荐指数
3
解决办法
7240
查看次数

递归阶乘程序的复杂性

查找数字阶乘的递归程序的复杂性是多少n?我的预感是可能的O(n).

complexity-theory factorial

27
推荐指数
4
解决办法
6万
查看次数

用于计算阶乘的快速算法

我发现这个页面描述了许多用于计算阶乘的算法.不幸的是,解释很简洁,我不想逐行筛选源代码来理解算法背后的基本原理.

任何人都能指出我对这些(或其他快速)计算因子的算法的更详细描述吗?

编辑: 此页面描述了素数因子分解的方法,这是所有性能最佳的因子算法共有的技术.它还包含Python中的一些很好的示例代码.作者链接到二进制分裂的描述,并参考了算法杂志("计算因子的复杂性")中的一篇文章,看起来很有希望,如果我只能得到它.

algorithm performance factorial

23
推荐指数
2
解决办法
3万
查看次数

OverflowError:long int太大,无法在python中转换为float

我试图在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但没有结果.

python overflow factorial

23
推荐指数
3
解决办法
5万
查看次数

计算任意大数的阶乘,显示所有数字

最近我在一次采访中被要求描述一种计算任意大数的阶乘的方法; 一种方法,我们获得答案的所有数字.

我搜索了不同的地方,并在几个论坛上询问.但我想知道是否有任何方法可以在不使用像GMP这样的库的情况下实现这一目标.

谢谢.

c++ factorial

22
推荐指数
3
解决办法
2万
查看次数

谁能解释这个算法用于计算大因子?

我遇到了以下计算大型因子(数字大到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)

c++ algorithm factorial

22
推荐指数
1
解决办法
2871
查看次数

在R中为什么阶乘(100)与prod(1:100)显示不同?

在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)

math r arithmetic-expressions factorial

22
推荐指数
3
解决办法
5898
查看次数

为什么Haskell中的因子计算要比Java中快得多

我遇到的一个编程问题涉及计算大数(最多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)

java haskell factorial

21
推荐指数
3
解决办法
4433
查看次数

阶乘递归算法的复杂性

今天在课堂上我的老师在黑板上写下了这个递归因子算法:

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.

之后,她取代jT(n-j) + j,并获得了T(1) + n-1.

她直接说那个n-1 = 2 (log 2 n-1),所以算法的成本是指数的.

我真的失去了最后两步.可以请有人向我解释一下吗?

complexity-theory big-o factorial

19
推荐指数
1
解决办法
4万
查看次数