用一个递归函数对1到n的阶乘求和

时间:2021-10-19 09:46:54

I need a single recursive function to compute the series S=1! + 2! + 3! + ...+n!.

我需要一个递归函数来计算级数S=1!+ 2 !+ 3 !+……+ n !。

I've done it using 2 functions.

我用了两个函数。

void main()
{ 
  int terms=4;
  printf(sum(terms));  //outputs 33
 /* 
    terms is the number of terms in the series
    e.g. terms=3 outputs 9, 4 outputs 33
 */
}

int fac(int n)   
{            
     return n<2 ? 1 : n * fac(n-1) ;
}   
int sum(int terms)   
{     
     return terms>0 ? (fac(terms) + sum(terms-1)):0 ;
}

4 个解决方案

#1


0  

This is the simplest I could get it:

这是我能得到的最简单的:

int sum_fac(int target, int counter) {
    return (target == counter) ? target : counter * (1 + sum_fac(target, counter + 1));
}

Which when called like this:

当这样称呼的时候:

int main() {
  for (int i = 1; i < 10; i++) {
    printf("%d: %d\n", i, sum_fac(i, 1));
  }
}

Outputs this:

输出:

1: 1
2: 3
3: 9
4: 33
5: 153
6: 873
7: 5913
8: 46233
9: 409113

#2


0  

You could have Something like this :

你可以有这样的东西:

int sumOfFac(int currentNumber, int lastSum, int lastFac, int maxNumber)
{
    int currentFac = currentNmber*lastFac;
    int currentSum = lastSum+currentFac;

    if(currentNumber < maxNumber)
    {
        return sumOfFac(currentNumber+1, currentSum, currentFac, maxNumber);
    }
    return currentSum;
}

Then you call that function the first time with :

然后你第一次调用这个函数

currentNumber = 1
lastSum = 0
lastFac = 1
maxNumber = your n

#3


0  

int main() {
   sum(1,terms,1,0);
}

int sum (int cur, int terms, int fac, int sumcur) {

    return terms == n? sumcur: sum(cur+1, terms, (cur==1)?1:cur*fac, sumcur+cur*fac);
}

#4


0  

a_1 = 1!
a_2 = 1!+2!
...
a_n = a_(n-1) + f_(n-1)*n

where f_i = i!. So basically we need two outputs from the dunction - a and f. Let's translate it to the code:

f_i =我!。所以基本上我们需要两个输出- a和f。让我们把它转换成代码:

int series(int n, int *f)
{
    int a, f1;
    if (n < 2)
    {
        *f = 1;
        return 1;
    }
    else
    {
        a = series(n-1, &f1);
        *f = f1 * n;
        return a + (*f);
    }
}

And then

然后

int main(void) {
    int dummy;
    printf("%d\n", series(1, &dummy));
    printf("%d\n", series(2, &dummy));
    printf("%d\n", series(3, &dummy));
    printf("%d\n", series(4, &dummy));
    return 0;
}

prints this:

打印:

1
3
9
33


Update:
To refactor further, we can say that f_n = a_n - a_(n-1), so f_(n-1) = a_(n-1) - a_(n-2). Then the recursive case will be:

更新:为了进一步重构,我们可以说f_n = a_n - a_(n-1),因此f_(n-1) = a_(n-1) - a_(n-2)。那么递归的情况是:

a_n = a_(n-1) + (a_(n-1) - a_(n-2))*n

This wont require the calculation of f, but need some extra bse cases and extra recursive call:

这并不需要计算f,但是需要一些额外的bse情况和额外的递归调用:

    int series(int n)
    {
        int a1, a2;
        if (n <= 1)
        {
            return 1;
        }
        else if (n==2)
        {
            return 3;
        }
        else
        {

            a1 = series(n-1);
            a2 = series(n-2);

            return a1 + (a1 - a2)*n;
        }
    }

#1


0  

This is the simplest I could get it:

这是我能得到的最简单的:

int sum_fac(int target, int counter) {
    return (target == counter) ? target : counter * (1 + sum_fac(target, counter + 1));
}

Which when called like this:

当这样称呼的时候:

int main() {
  for (int i = 1; i < 10; i++) {
    printf("%d: %d\n", i, sum_fac(i, 1));
  }
}

Outputs this:

输出:

1: 1
2: 3
3: 9
4: 33
5: 153
6: 873
7: 5913
8: 46233
9: 409113

#2


0  

You could have Something like this :

你可以有这样的东西:

int sumOfFac(int currentNumber, int lastSum, int lastFac, int maxNumber)
{
    int currentFac = currentNmber*lastFac;
    int currentSum = lastSum+currentFac;

    if(currentNumber < maxNumber)
    {
        return sumOfFac(currentNumber+1, currentSum, currentFac, maxNumber);
    }
    return currentSum;
}

Then you call that function the first time with :

然后你第一次调用这个函数

currentNumber = 1
lastSum = 0
lastFac = 1
maxNumber = your n

#3


0  

int main() {
   sum(1,terms,1,0);
}

int sum (int cur, int terms, int fac, int sumcur) {

    return terms == n? sumcur: sum(cur+1, terms, (cur==1)?1:cur*fac, sumcur+cur*fac);
}

#4


0  

a_1 = 1!
a_2 = 1!+2!
...
a_n = a_(n-1) + f_(n-1)*n

where f_i = i!. So basically we need two outputs from the dunction - a and f. Let's translate it to the code:

f_i =我!。所以基本上我们需要两个输出- a和f。让我们把它转换成代码:

int series(int n, int *f)
{
    int a, f1;
    if (n < 2)
    {
        *f = 1;
        return 1;
    }
    else
    {
        a = series(n-1, &f1);
        *f = f1 * n;
        return a + (*f);
    }
}

And then

然后

int main(void) {
    int dummy;
    printf("%d\n", series(1, &dummy));
    printf("%d\n", series(2, &dummy));
    printf("%d\n", series(3, &dummy));
    printf("%d\n", series(4, &dummy));
    return 0;
}

prints this:

打印:

1
3
9
33


Update:
To refactor further, we can say that f_n = a_n - a_(n-1), so f_(n-1) = a_(n-1) - a_(n-2). Then the recursive case will be:

更新:为了进一步重构,我们可以说f_n = a_n - a_(n-1),因此f_(n-1) = a_(n-1) - a_(n-2)。那么递归的情况是:

a_n = a_(n-1) + (a_(n-1) - a_(n-2))*n

This wont require the calculation of f, but need some extra bse cases and extra recursive call:

这并不需要计算f,但是需要一些额外的bse情况和额外的递归调用:

    int series(int n)
    {
        int a1, a2;
        if (n <= 1)
        {
            return 1;
        }
        else if (n==2)
        {
            return 3;
        }
        else
        {

            a1 = series(n-1);
            a2 = series(n-2);

            return a1 + (a1 - a2)*n;
        }
    }