English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Fibonacci numbers in the Fibonacci sequence grow exponentially, for500 or1000 such large numbers may be very large. To handle such numbers, long data types are not enough. BigInteger can easily handle a large number of digits. BigInteger is very useful when the data calculated exceeds the limit of the available primitive data types. See the following example to get the Fibonacci number100 and1000.
import java.math.BigInteger; public class Tester { public static void main(String args[]) { System.out.println("Fibonacci of 100: "); System.out.println(fibonacci(100)); System.out.println("Fibonacci of 1000: "); System.out.println(fibonacci(1000)); } private static BigInteger fibonacci(int n) { BigInteger a = BigInteger.ZERO; BigInteger b = BigInteger.ONE; BigInteger c = BigInteger.ONE; for (int i =2 ; i <= n ; i++) { c = a.add(b); a = b; b = c; } return a; } }
Fibonacci of 100: 218922995834555169026 Fibonacci of 1000: 2686381002448535938614672720214292396761660931898695 2340123175997617981700247881689338369654483356564191 8278561614433563129766736422103503246348504103776803 67334151172899169723197082763985615764450078474174626