java求黄金比例,有趣的黄金分割,黄金比例和阶乘,通过循环与递归实现。,黄金分割阶乘,import java....

有趣的黄金分割,黄金比例和阶乘,通过循环与递归实现。,黄金分割阶乘,import java.import java.math.*;import static java.math.BigInteger.*;/** * Fun with Fibonacci, Golden Ratio and Factorials, with loops vs recursives. */public class FiboFact { /** * Fibonacci in a loop, no recursion. */ private static int fibLoop(int n) { if(n < 2) return n; int previous = 0; int next = 1; for(int i = 1; i < n; i++) { int save = next; next += previous; previous = save; } return next; } /** * Fibonacci recursive. Although sleek, short and with nothing that could go wrong, there is a serious * performance issue for large N: there is an exponential explosion of reevaluations and Java does not provide memoization without * a dedicated effort to it. Therefore, for an N, N-2 will be evaluated by N and N-1. N-3 will be evaluated by N, N-1 and N-2, * and so on. */ private static int fibRecursive(int n) { // uncomment the following line to see the exponential explosion of reevaluations: //System.out.printf("'); return n < 2 ? n : fibRecursive(n-1) + fibRecursive(n-2); // this fork is the cause of exponential explosion } /** * Factorial in a loop with long, good till n = 20. With int, the biggest n would be 12. */ private static long factLoop(long n) { if(n < 0) throw new IllegalArgumentException("Factorial operation illegal for " + n); if(n == 0) return 1; long result = 1; for(int i = 1; i <= n; i++) result *= i; return result; } private static long factRecursive(long n) { if(n < 0) throw new IllegalArgumentException("Factorial operation illegal for " + n); return n <= 1 ? 1 : n * factRecursive(n - 1); } /** * Big Integer for factorials of integers greater than 20. */ private static BigInteger factBdLoop(final BigInteger n) { if(n.compareTo(ZERO) < 0) throw new IllegalArgumentException("Factorial operation illegal for " + n); if(n.compareTo(ONE) <= 0) return ONE; BigInteger result = ONE; for(int i = 1; i <= n.intValue(); i++) result = result.multiply(BigInteger.valueOf(i)); return result; } private static BigInteger factBdRecursive(final BigInteger n) { if(n.compareTo(ZERO) < 0) throw new IllegalArgumentException("Factorial operation illegal for " + n); return n.compareTo(ONE) <= 0 ? ONE : n.multiply(factBdRecursive(n.subtract(ONE))); } public static void main(String[] args) { int prev = 0; for(int n = 0; n <= 15; n++) { int f = fibLoop(n); System.out.printf("%nn=%2d, loop: %d, recurse: %d", n, f, fibRecursive(n)); if(prev != 0) System.out.printf(", golden ratio= %19.17f", Double.valueOf(f)/Double.valueOf(prev)); prev = f; } final int goldenBase = 46; // max fib that fits in an int // for 92, 7540113804746346429/4660046610375530309=1.618033988749894848204586834365638117699 final BigDecimal fiBigger = BigDecimal.valueOf(fibLoop(goldenBase)); final BigDecimal fiSmaller = BigDecimal.valueOf(fibLoop(goldenBase - 1)); // for fib[46]/fib[45] we get 17 correct digits of golden ratio System.out.printf("%nGolden Ratio of %,.0f/%,.0f: %19.17f", fiBigger, fiSmaller, fiBigger.divide(fiSmaller, 44, RoundingMode.HALF_UP)); for(int n = 0; n <= 21; n++) { // 21! already blows the Long range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 inclusive // 20! is the last valid factorial for Long; for int it's 12 System.out.printf("%nn=%2d, loop: %,25d; recurse: %,25d", n, factLoop(n), factRecursive(n)); } System.out.printf(" <<<< WOOPS!! Too big for a Long!"); for(int n = 0; n < 31; n++) { // with big integer the limit is memory final BigInteger bigN = BigInteger.valueOf(n); System.out.printf("%nn=%2d, loop: %,45d; recurse: %,45d", n, factBdLoop(bigN), factBdRecursive(bigN)); } System.out.printf("%nLong: min=%,d, max=%,d", Long.MIN_VALUE, Long.MAX_VALUE); }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值