algorithm - My puzzle on Project Euler # 25 about java.lang.NullPointerException -


i working on project euler #25. intending save fibonacci number in biginteger array. however, there throws nullpointerexception, don't know why , how avoid it. know there more simple algorithm solve question. want know mistake, thank much! here question states: fibonacci sequence defined recurrence relation:

fn = fn−1 + fn−2, f1 = 1 , f2 = 1. hence first 12 terms be:

f1 = 1 f2 = 1 f3 = 2 f4 = 3 f5 = 5 f6 = 8 f7 = 13 f8 = 21 f9 = 34 f10 = 55 f11 = 89 f12 = 144 12th term, f12, first term contain 3 digits.

what first term in fibonacci sequence contain 1000 digits?

my code below:

  private static biginteger[] fibonacci;     public static void main(string[] args) {         for(int = 0; fibonacci[fibonacci.length-1].tostring().length() < 1000; i++){             if(i == 0)                 fibonacci[i] = biginteger.zero;             if(i < 3)                 fibonacci[i] = biginteger.valueof(1);             else                 fibonacci[i] = fibonacci[i - 1].add(fibonacci[i - 2]);         }         system.out.println(fibonacci.length);     } 

you have not initialized array fibonacci. need allocate memory before using array.

private static biginteger[] fibonacci = new biginteger[1000]; // number x 

i recommend using list instead of array given don't know size of array.

private static list<biginteger> fibonacci = new arraylist<biginteger>(); 

also, if using arrays, fibonacci[fibonacci.length - 1] not work because checking position 999 if you're initializing array size 1000. work, have use arraylist , can use

fibonacci[fibonacci.size() - 1] 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -