使用 MacLaurin 系列用 Java 递归计算 e^x [英] Recursively calculate e^x with Java using MacLaurin Series

查看:47
本文介绍了使用 MacLaurin 系列用 Java 递归计算 e^x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个递归的java方法,该方法将使用签名计算名为e(x,n)的e^x,

I need to write a recursive java method that will compute e^x called e(x,n) with the signature,

public static double eThree(double x, long n) 

并且它必须使用 MacLaurin 级数来计算 e^x,这是以下观察:

and it must use the MacLaurin series to compute e^x, which is the following observation:

1 + x(1 + x/2( 1 + x/3(1 + x/4(1 + ... ))))
e(x,0)= 1 A call to this would return a result of 1

在之前的一些帮助下,我能够制作一个不需要这种格式的文件,但我不确定要使用上述格式需要编写什么代码.谢谢大家,非常感谢您提供的任何帮助!

With some help earlier, I was able to make one that didn't require this format, but I'm not sure what I would code in order to use the format above. Thanks guys, really appreciate any help that will be given!

推荐答案

这对您有用吗?我相信它实现了算法,但它不会产生 x^n.

Does this work for you? I believe it implements the algorithm but it does not produce x^n.

public static double eThree(double x, long n) {
    return eThree(x, n, 1);
}

private static double eThree(double x, long n, int div) {
    double d = 1;
    if (n > 0) {
        d = d + (x / div) * (eThree(x, n - 1, div + 1));
    }
    return d;
}

似乎认为:

2^0 = 1.0
2^1 = 3.0
2^2 = 5.0
2^3 = 6.333333333333333
2^4 = 7.0
2^5 = 7.266666666666667
2^6 = 7.355555555555555
2^7 = 7.3809523809523805
2^8 = 7.387301587301588
2^9 = 7.388712522045855

这篇关于使用 MacLaurin 系列用 Java 递归计算 e^x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆