java - Fibonacci sequence long[] array throws negative numbers after index 92 -


i'm testing code insert fibonacci sequence in long[] array:

public class test {     public static void fibonacci(int n){         long[] array = new long[n];         array[0]=1;         (int = 1; < n; i++) {             if (i==1) {                 array[i]=i;             }             else {                 array[i] = array[i-2] + array[i-1];             }         }         system.out.println(array[n-3]+"    "+array[n-2]); // verify sum         system.out.println(array[n-1]);     }     public static void main(string[] args) {          scanner scan = new scanner(system.in);          system.out.print("insert fibonacci sequence index: ");         int n = scan.nextint();          fibonacci(n);     } } 

however, after position 92, starts throwing wrong or negative numbers. i'm using fibonacci calculator verify numbers , until 92 it's correct. i've seen questions here problem , answers integer overflow, , should use long, using.

is 93th number on limit of long type? should use instead reach 100 or bigger numbers , still manage array?

you overflowing range of long. can use biginteger (and extract array[1] loop); something1 like

public static void fibonacci(int n) {     biginteger[] array = new biginteger[n];     array[0] = array[1] = biginteger.one;      (int = 2; < n; i++) {         array[i] = array[i - 2].add(array[i - 1]);     }     system.out.println(array[n - 3] + "    " + array[n - 2]); // verify sum     system.out.println(array[n - 1]); } 

1also, please follow java naming conventions. method names start lower case letter, fibonacci looks class name.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo