Writing Fibonacci sequence in Java starting from certain term -


i trying write fibonacci sequence generator based on java. saw many examples on internet instance:

public class fibonacci {      public static void main(string[] args) {         generatefibonacci(20);  // generate first 20 fibonacci numbers     }      public static void generatefibonacci(long limit) {         long first = 0;         long second = 1;         system.out.print(first+", "+second);         for(long i=1;i<limit;i++) {             long next = first + second;             system.out.print(", "+next);             first = second;             second = next;         }         system.out.println();     }  } 

and works fine. problem not want generate first 20 numbers, need specify starting point. example:

public list<long> generatefibonacci(long startfrom, long numberterms) {  } 

doing:

generatefibonacci(5, 10); 

should output: 8l
13l 21l 34l 55l 89l 144l 233l 377l

i have tried following code doesnt seem performing desired action:

public class example {     public static void main(string[] args) {         generatefibonacci(10, 5);      }      public static void generatefibonacci(long limit, long startpoint) {         long first = 0;         long second = 1;         long endpoint = startpoint + limit;         system.out.print(first + ", " + second);         (long = 1; < endpoint; i++) {             long next = first + second;             if (i > startpoint) {                 system.out.print(", " + next);             }             ;             system.out.print(", " + next);             first = second;             second = next;         }     } } 

any ideas on how achieve in efficient way?

as said output must this

 8                                                                                                                                                                     [13, 21, 34, 55, 89, 144, 233, 377]  

and want return list of fibonacci numbers

you can return list want in way !

public static void main(string[] args) {     list<long> list = generatefibonacci(10, 5);     system.out.println(list.size());          system.out.println(arrays.tostring(list.toarray()));      }  public static list<long>  generatefibonacci(long limit, long startpoint) {     list<long> list = new arraylist<>();      long first = 0;     long second = 1;     long endpoint = startpoint + limit - 1;     //system.out.print(first + ", " + second);     if (startpoint==0){             list.add(0);             list.add(1);     }else if (startpoint ==1){             list.add(1);     }     (long = 2; < endpoint; i++) {         long next = first + second;         if (i > startpoint) {             // system.out.println(next);             list.add(next);         }          first = second;         second = next;     }      return list; } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -