0

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.

5

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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+" ");         
        }
    }
}

Comments

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);
}

Comments

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.

Comments

0
int x = 10, i=0, f=1, s=1, o = 1;
while (x > 0) {
    if(i!=0 && i!=1){
        o=f+s;
        f=s;
        s=o;
    }
    System.out.print(o);
    x--;
    i++;
}

1 Comment

1) Wrong print function. 2) A for loop with only i changing from 0 to 10 will do just fine here. x seems unnecessary.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.