本文以實例形式講述了Java基於高精度整型實現fibonacci數列的方法,分享給大家供大家參考之用。具體方法如下:
package com.java.learning.recursion; import java.math.*; public class MainClass { public static void main(String args[]){ for(int i = 0; i < 100; i++){ f(i+1); } } public static BigInteger f(long n){ if(n <= 2){ return new BigInteger("1"); }else{ BigInteger n1 = new BigInteger("1"); BigInteger n2 = new BigInteger("1"); BigInteger temp = new BigInteger("0"); for(long i = 0; i < n -2; i++){ temp = n1.add(n2); n1 = n2; n2 = temp; } System.out.println("第" + n + "項為:" + n2); return n2; } } }
希望本文所述對大家的Java程序設計有所幫助。