当前位置:   article > 正文

【JavaBigDecimal练习】利用BigDecimal精确计算欧拉数_用java求小数点后1000位

用java求小数点后1000位

利用BigDecimal精确计算欧拉数小数点后面1000位

相信大家对于泰勒展开已经稔熟于心,接下来我们就利用下列的泰勒展开: e x = 1 + 1 1 ! x + 1 2 ! x 2 + 1 3 ! x 3 + . . . . . . . + 1 n ! x n \qquad e^x = 1+\frac{1}{1!}x+\frac{1}{2!}x^2+\frac{1}{3!}x^3+.......+\frac{1}{n!}x^n ex=1+1!1x+2!1x2+3!1x3+.......+n!1xn
当展开式满足下列条件时: x = 1 \qquad x =1 x=1

可推出如下结论:
e 1 = 1 0 ! + 1 1 ! + 1 2 ! + . . . . . . . . . . + 1 ( n − 1 ) ! + 1 n ! \qquad e^1 = \frac{1}{0!}+\frac{1}{1!}+\frac{1}{2!}+..........+\frac{1}{(n-1)!}+\frac{1}{n!} e1=0!1+1!1+2!1+..........+(n1)!1+n!1

用代码进行实现

// An highlighted block
package Exe5;

import java.math.BigDecimal;

public class Demo {

    /** main-method */

    public static void main(String[] args) {
        int num1 = 10000;
        BigDecimal temp1 = new BigDecimal(num1);
        System.out.println(valueOfEuler(temp1));
        System.out.println("===============================");
        int num2 = 20000;
        BigDecimal temp2 = new BigDecimal(num2);
        System.out.println(valueOfEuler(temp2));
        System.out.println("===============================");
        int num3 = 30000;
        BigDecimal temp3 = new BigDecimal(num3);
        System.out.println(valueOfEuler(temp3));
    }
    /**
     * approximately value of nature Euler scale
     *
     * @param num
     * @return value of Euler
     */
    public static BigDecimal valueOfEuler(BigDecimal num) {
        //初始化自然对数也就是欧拉数
        double euler = 0;
        //初始化赋值1 用来和num比较以便控制累加次数
        double temp = -1;
        //temp2作用为提供 num个1来累加
        double temp2 = 1;
        //将double型欧拉数字转化为BigDecimal
        //将temp也转化为BigDecimal
        //将temp2也转化为BigDecimal
        BigDecimal newEuler = new BigDecimal(euler);
        BigDecimal newTemp = new BigDecimal(temp);
        BigDecimal newTemp2 = new BigDecimal(temp2);
        //运用compareTo()方法控制累加次数
        do {
            // newTemp++
            newTemp = newTemp.add(newTemp2);
            // e = 1/0!+ 1/1!+1/2!+......+1/num!
            newEuler = newEuler.add(newTemp2.divide(ComputeFactorial(newTemp), 1000, BigDecimal.ROUND_UP));
            //直到newTemp自己加到与num相等后
        } while (newTemp.compareTo(num) == -1);
        //返回欧拉数
        return newEuler;
    }

    /**
     * 大数阶乘方法
     */
    public static BigDecimal ComputeFactorial(BigDecimal num) {
        //初始变量 值为1
        int s = 1;
        //改变数据类型
        BigDecimal begin = new BigDecimal(s);
        //如果导入的参数num > begin,则返回1
        if (num.compareTo(begin) == 1) {
            //结果result = num*(num-1)
            BigDecimal result = num.multiply(num.subtract(begin));
            //返回结果 result = num * (num - 1) * (num - 2) * (num -3) .....* 1 两两一组以此递归
            return result.multiply(ComputeFactorial(num.subtract(begin.add(begin))));
        }
        //如果相等过后返回begin begin的值为1
        else {
            return begin;
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

输出语句

由于计算量很大目前只展示

int num = 10000;
BigDecimal newNum = new BigDecimal(num);
System.out.println(ValueOfEuler(newNum));
  • 1
  • 2
  • 3

结果展示

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/249193
推荐阅读
相关标签
  

闽ICP备14008679号