Max Integer的递归

时间:2014-06-04 17:49:54

标签: c ansi factorial

我正在编写一个程序来计算数字的阶乘。我正在使用递归来解决这个问题。我遇到的问题是,一旦我达到13号,它将因为INT的限制而丢弃垃圾数量。我想要做的是实现一种方法来捕获错误(没有硬编码,在x = 13时它必须停止,而不是输出)。这是我的尝试:

#include <stdio.h>

int factorial( int n)
{
    printf("Processing factorial( %d )\n", n);

    if (n <= 1)
    {
        printf("Reached base case, returning...\n");
        return 1;
    }
    else
    {
        int counter = n * factorial(n-1);       //Recursion to multiply the lesser numbers

        printf("Receiving results of factorial( %d ) =  %d * %d! = %d\n", n, n, (n-1), counter);

        if( counter/n != factorial(n-2) ) //my attempt at catching the wrong output
        {
            printf("This factorial is too high for this program ");
            return factorial(n-1);
        }
        return counter;


        printf("Doing recursion by calling factorial (%d -1)\n", n);
    }


}


int main()
{
    factorial(15);
}

这个问题是程序现在永远不会终止。它继续循环并向我扔掉随机结果。

由于我无法回答我自己的问题,我将使用我的解决方案进行编辑:

int jFactorial(int n)
{
    if (n <= 1)
    {
        return 1;
    }
    else
    {
        int counter = n *jFactorial(n-1);
        return counter;
    }
}

void check( int n)
{
    int x = 1;
    for(x = 1; x < n+1; x++)
    {
        int result = jFactorial(x);
        int prev = jFactorial(x-1);
        if (((result/x) != prev) || result == 0 )
        {
            printf("The number %d makes function overflow \n", x);
        }
        else
        {
            printf("Result for %d is %d \n", x, result);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

更好的方法:

if (n <= 1) {
   return 1;
} else {
    int prev_fact = factorial(n - 1);
    if (INT_MAX / prev_fact < n) { /* prev_fact * n will overflow */
        printf("Result too big");
        return prev_fact;
    } else {
        return prev_fact * n;
    }
}

使用更准确的检查(我希望),乘法是否会溢出,并且不再添加factorial的调用。

答案 1 :(得分:1)

更新

仔细观察之后,事实证明我错过了gmp也是为C实现的事实。这是C中的解决方案

我已经能够在我的macbook pro上运行它,使用自制软件来安装gmp(brew isntall gmp

#include <gmp.h>

#include <stdio.h>

void factorial(mpz_t ret, unsigned n) {
  if (n <= 1) {
    mpz_set_ui(ret, 1);//Set the value to 1
  } else {
    //multiply (n-1)! with n
    mpz_t ret_intermediate;
    mpz_init (ret_intermediate);//Initializes to zero
    factorial(ret_intermediate, n-1);
    mpz_mul_ui(ret, ret_intermediate, n);
  }

  return;
}

int main(){
  mpz_t result;
  mpz_init (result);
  factorial(result, 100);
  char * str_result = mpz_get_str(NULL, 10, result);
  printf("%s\n", str_result);
  return 0;
}

原始答案

快速谷歌搜索后,我找到了以下解决方案。请注意,这是一个C ++解决方案。我简要地说明了如何在底部的ANSI C中做同样的事情。

Big numbers library in c++

https://gmplib.org/这个c ++库可以处理任意大的数字。

结帐https://gmplib.org/manual/C_002b_002b-Interface-General.html

整个代码看起来像......

#include <gmpxx.h>

#include <iostream>

mpz_class factorial(unsigned n) {
  if (n <= 1) return mpz_class(1);

  return mpz_class(n) * factorial(n-1);
}

int main(){
  mpz_class result = factorial(100);
  std::string str_result = result.get_str();
  std::cout << str_result << std::endl;
  return 0;
}

ANSI C版

您可以使用ansi C实现相同的功能,其结构可以保存扩展的数字列表(使用链接列表或任何其他可扩展的arraylist容器),并且您只需要实现三种方法......初始化,乘法并转换为字符串。