Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 689 Bytes

Ex_1_1_20.md

File metadata and controls

43 lines (33 loc) · 689 Bytes
title date draft tags categories
算法4 Java解答 1.1.20
2019-03-02 19:38:47 +0800
false
JAVA
技术
归档

1.1.20

问题:

1.1.20 Write a recursive static method that computes the value of ln (N !)

分析:

  public static double ln(int n){
    if(n==1) return 0;
    return ln(n-1)+Math.log(n);
  }

  public static void main(String[] args) {
    int N = 5;
    double ans = ln(N);
    StdOut.printf("ln(%d!) = %f\n", N, ans);
    double ret = 0.0;
    for (int i = 1; i <= N; i++) {
      ret+=Math.log(i);
    }
    System.out.println("check: " + ret);
//    ln(5!) = 4.787492
//    check: 4.787491742782046
  }
``

## 参考: