2

I am trying to practice JAVA by coding a Fibonacci sequence in which the user can declare the length of the Fibonacci starting from 0 to n. Here is my code:

public class test {
public static void main(String args[]) throws IOException {
    BufferedReader pao = new BufferedReader(
            new InputStreamReader(System.in));

    System.out.print("Enter number: ");
    String enter = pao.readLine();
    int ent = Integer.parseInt(enter);

    int total1 = 0;
    int total2 = 1;
    int total3 = 0;

    for (int a = 0; a < ent; a++) {
        if (a <= 1) {
            System.out.print(" " + total3);
            total3 = total1 + total2;

        } else if (a == 2) {
            System.out.print(" " + total3);
        } else {
            total1 = total2;
            total2 = total3;
            total3 = total1 + total2;
            System.out.print(" " + total3);

        }
    }

}
}  

Is there another way to do this in a much more simpler, shorter and "Nicer" way? still without using arrays. Thank you in advance.

Scar
  • 379
  • 4
  • 21
  • 5
    Should go to http://codereview.stackexchange.com – user2336315 Nov 07 '14 at 18:20
  • using recursion, see http://en.wikipedia.org/wiki/Fibonacci_number –  Nov 07 '14 at 18:23
  • @user2336315 thank you for that info. I wasn't aware that there's a place for code reviews. – Scar Nov 07 '14 at 18:53
  • @RC. Thank you. I'm new to JAVA so I'm still not familiar to some methods – Scar Nov 07 '14 at 18:55
  • @Scar: just be sure to check the [What topics can I ask about here?](http://codereview.stackexchange.com/help/on-topic) page for Code Review first. (For that matter, since you're new here, you should read the [corresponding page for Stack Overflow](http://stackoverflow.com/help/on-topic) as well). – Daniel Pryden Nov 07 '14 at 19:22

4 Answers4

3

You can use recursive fibonacci but it will increase your runtime from O(n) to O(2^n) it's like bellow

int fib(int n) {
   if (n <= 1)
      return n;
   return fib(n-1) + fib(n-2);
}

and there is another way that decrease your runtime to O(log n) but it use arrays (Using power of the matrix {{1,1},{1,0}}) you can read about it here. also for above recursion if you use array you can change runtime to O(n) again by method call dynamic programming.

Karo
  • 273
  • 4
  • 16
1
public class Fibo {
    public static void main(String[] args) {
        int n = 12;
        int a = 0;
        int b = 1;

        System.out.print(a + " ");
        System.out.print(b + " ");
        for (int i = 0; i < n-2; i++) {
            int temp = a + b;
            a = b;
            b = temp;
            System.out.print(temp+" ");         
        }
    }
}
pushkin
  • 9,575
  • 15
  • 51
  • 95
0

Using recursion :

public int fibonacci(int n)  {
    if(n == 0)
        return 0;
    else if(n == 1)
      return 1;
   else
      return fibonacci(n - 1) + fibonacci(n - 2);
}
Vikas Verma
  • 3,626
  • 6
  • 27
  • 40
0

Your iterative solution is fine (although there are other nitpicks I would make if this were a code review). The recursive implementation would win you more style points, but it is substantially slower and more memory-intensive.

For maximum style points(*), you can also use Binet's formula for a closed-form solution for the Fibonacci sequence.

(*) Don't do this in production code, though, unless you're really, really sure you need it.

Community
  • 1
  • 1
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135