title | date | draft | tags | categories | |||
---|---|---|---|---|---|---|---|
算法4 Java解答 1.1.20 |
2019-03-02 19:38:47 +0800 |
false |
|
|
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
}
``
## 参考: