本文以实例形式讲述了Java基于高精度整型实现fibonacci数列的方法,分享给大家供大家参考之用。具体方法如下:
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
|
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程序设计有所帮助。