使用递归e = 1 + 1/1!+1/2!+1 / 3!+添加分数

时间:2013-11-23 14:35:32

标签: java recursion fractions

我需要编写一个递归方法来计算以下系列:   e = 1 + 1/1!+1/2!+1/3!+ ...

这是我到目前为止所做的。

public static void main(String[] args)
{  System.out.println("enter n :");
   int n =scan.nextInt();
   double h = fact(n);
   System.out.println(" e = ");
}
public double fact(int n)
 {
    if (n == 1)
        return 1;
    else
        return ???;
  }
}

4 个答案:

答案 0 :(得分:3)

所以,假设您正在使用的n输入是您添加的最小分数的起点...

(例如,给定n = 10,您想通过1添加1/10

然后你需要设置你的方法,这样当你致电fact(10)时,它会返回1/10加上fact(9)的结果的总和,或更一般地,{{ 1}}

所以,你正在寻找这样的东西:

1/n + fact(1/n-1);

另外,请注意对基本案例的更改。当public double fact(int n) { if (n < 0) { return 0.0; } else if (n == 0) { return 1.0; } else { return (1.0/n + fact(n-1)) } } 时,我们只是n < 0,因为如果我没记错的话,任何负数的阶乘都是0,对吗?

同时,基本情况应为return 0.0,而不是n==0。您的系列以n == 1开头。请注意,1 + 1/1不是11/0,而只是1/nothing1/11/n时,我们无法返回n。为了正确计算系列,我们必须在0的情况下添加第一个返回系列的第一个元素。

请记住,和所有递归函数一样,非常大的n = 0值会导致堆栈溢出。

答案 1 :(得分:0)

以下是一些资源:

Math is fun

  

“是的,你可以!但是你需要进入一个叫做”Gamma“的主题   功能“,超出了这个简单的页面。

     

半因素

     

但我可以告诉你,半数(1/2)的阶乘是正方形的一半   pi的根=(½)√π,因此一些“半整数”因子是:“

更具体地说,您需要Gamma Function

Apache commons具有此功能的实现。

关于Math Exchange

的讨论

这是Princeton

的实现
public class Gamma {

   static double logGamma(double x) {
      double tmp = (x - 0.5) * Math.log(x + 4.5) - (x + 4.5);
      double ser = 1.0 + 76.18009173    / (x + 0)   - 86.50532033    / (x + 1)
                       + 24.01409822    / (x + 2)   -  1.231739516   / (x + 3)
                       +  0.00120858003 / (x + 4)   -  0.00000536382 / (x + 5);
      return tmp + Math.log(ser * Math.sqrt(2 * Math.PI));
   }
   static double gamma(double x) { return Math.exp(logGamma(x)); }

   public static void main(String[] args) { 
      double x = Double.parseDouble(args[0]);
      System.out.println("Gamma(" + x + ") = " + gamma(x));
      System.out.println("log Gamma(" + x + ") = " + logGamma(x));
   }

}

答案 2 :(得分:0)

递归地计算e ^ n非常昂贵。它是O(n ^ 2)并且很难知道何时停止。相反,我建议你反复进行。

static final int runs = 20000;
static volatile int exp = 1;
static volatile int n = 18;
static volatile double dontOptimiseAway;

public static void main(String[] args) throws InterruptedException {
    System.out.println("Math.exp(1)=" + Math.exp(1));
    System.out.println("exp_iter(18)=" + exp_iter(18));
    System.out.println("exp_recurse(18)=" + exp_recurse(18));

    for (int t = 0; t < 3; t++) {
        System.out.printf("exp(1), exp_iter(18), exp_recurse(18) took %,d / %,d / %,d ns on average%n",
                timeMathExp(), timeExpIter(), timeExpRecurse());
    }
}

public static long timeMathExp() {
    long start = System.nanoTime();
    for (int i = 0; i < runs; i++)
        dontOptimiseAway = Math.exp(exp);
    return (System.nanoTime() - start) / runs;
}

public static long timeExpIter() {
    long start = System.nanoTime();
    for (int i = 0; i < runs; i++)
        dontOptimiseAway = exp_iter(n);
    return (System.nanoTime() - start) / runs;
}

public static long timeExpRecurse() {
    long start = System.nanoTime();
    for (int i = 0; i < runs; i++)
        dontOptimiseAway = exp_recurse(n);
    return (System.nanoTime() - start) / runs;
}

public static double exp_iter(int n) {
    double exp = 0, x = 1;
    for (int i = 2; i <= n; i++)
        exp += (x /= i);
    return 2 + exp;
}

public static double exp_recurse(int n) {
    return n <= 0 ? 1 : 1.0 / fact(n) + exp_recurse(n - 1);
}

public static double fact(int n) {
    return n <= 1 ? 1 : n * fact(n - 1);
}

打印

Math.exp(1)=2.718281828459045
exp_iter(18)=2.718281828459045
exp_recurse(18)=2.7182818284590455
exp(1), exp_iter(18), exp_recurse(18) took 111 / 191 / 760 ns on average
exp(1), exp_iter(18), exp_recurse(18) took 75 / 78 / 558 ns on average
exp(1), exp_iter(18), exp_recurse(18) took 69 / 66 / 552 ns on average

答案 3 :(得分:0)

编写如下代码,然后从主类中调用它。

public static double recursiveFun(double value){
        if (value==1)
            return 1.0;


        if (value==2){

            return (1/(value-1) + 1/value);

        }

        else
            return recursiveFun(value-1) + 1/value;

    }