Solution for fibonacci series

import java.util.ArrayList;
public class PrimeDirective{


  public ArrayList<Integer> fib(int x){
    ArrayList<Integer> fibonacci = new ArrayList<Integer>();
    fibonacci.add(0);
    fibonacci.add(0);
    if(x < 2) return fibonacci;
    for(int i = 2;i<=x;i++){
      int sum = (i - 1) + (i - 2);
      fibonacci.add(sum);
    }
      
      return fibonacci;
  }

  public static void main(String[] args){
    PrimeDirective pd = new PrimeDirective();


    System.out.println(pd.fib(3));


  }
}
1 Like

Very nice :slight_smile:

How does it handle negative input though? :wink:

One question, how could both first and second term be zero? They will always add up to zero.

1 Like

because in fibonacci series first 2 term are 0. So I have added these to elements manually and I have started my loop from 2. If I start my loop from 0 then first 2 elements will be negative.

but i guess fibonacci series only takes positive input.
but in my code if user inputs negative no then it will return [0,0].(I have not done anything for negative input. lol).

Haha, I was not too serious, getting fibonacci to work is great! But usually in code challenges you have to lay out handling of cases that break the code like this. It’s a useful thing to think about in your own programs and it’s a good issue to openly process if you’re coding in an interview.

If you have a base case that immediately identifies it’s an invalid input, then you don’t have to waste the time of your code processing dud input. If the code were a lot larger it could become a pain…

import java.util.ArrayList;
public class PrimeDirective{


  public ArrayList<Integer> fib(int x){
    ArrayList<Integer> fibonacci = new ArrayList<Integer>();
    if(x<0) System.out.print("invalid input");
    if(x < 2 && x > -1) {
      fibonacci.add(0);
      fibonacci.add(0);
      return fibonacci;
    }
    if(x < 2 && x > -1) return fibonacci;
    for(int i = 2;i<=x;i++){
      int sum = (i - 1) + (i - 2);
      fibonacci.add(sum);
    }
      
      return fibonacci;
  }

  public static void main(String[] args){
    PrimeDirective pd = new PrimeDirective();


    System.out.println(pd.fib(-1));


  }
}

what about this?

1 Like

Yea that does the trick, it’s a simple but crucial step (and in other scenarios it won’t be as simple as catching the negative :sweat_smile: I speak from a place of suffering…)

1 Like

The first two terms should be either 0 and 1, or 1 and 1. Both will yield the same terms but one will be shifted by one from the other.

0 1 1 2 3 5 8 13
1 1 2 3 5 8 13 21

I don’t know what you are saying but let me explain what my program does.
firstly lets understand what a fibonacci series is: it is a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers.
for ex f2= 1 + 0. f3 = 2+1
now what my program does: I have created a method that returns an Arraylist and has a single parameter(int x). Now I have added the first two term of fibonacci series manually which are [0,0]. after that I have created a loop for fibonacci series which you can see.

Now still if you have a doubt please reply.

How do we get 1 from adding 0 and 0?

you dont get 1 by 0 and 0 see
f0 = 0
f1 = 0+0
f2 = 0+1=1

got it?
if not then read about fibonacci series on wifipedia

Whoa there. He’s just trying to help out.

My understanding is that the first two terms are 0, 1 (or in some old books, 1 and 1, which is corroborated by wikipedia).

:sweat_smile: my bad I thought the first two terms are 0,0. yes @mtf you are right fir two terms are 0,1 my bad. Sorry for the inconvenience.

1 Like

No problem. Made you look…

2 Likes