Skip to content

Instantly share code, notes, and snippets.

@BT-ICD
Created May 19, 2021 14:12
Show Gist options
  • Save BT-ICD/54c7dd33a0d1c3e34f7003106efda82e to your computer and use it in GitHub Desktop.
Save BT-ICD/54c7dd33a0d1c3e34f7003106efda82e to your computer and use it in GitHub Desktop.
Example: Fibonacci Sequence
/**
* Example: Fibonacci Sequence
* In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.
* Reference: https://en.wikipedia.org/wiki/Fibonacci_number
* Sample Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
* */
public class FibonacciNumbersDemo {
public static void main(String[] args) {
int f0, f1, fn;
int num = 20;
f0=0;
f1=1;
for (int i = 0;i<num;i++){
// if(i==0){
// fn=f0+f0;
// }
// else if(i==1){
// fn=f1+f0;
// f1=fn;
//
// }
// else{
// fn=f1+f0;
// f0=f1;
// f1=fn;
// }
System.out.print(f0+"\t");
fn=f0+f1;
f0=f1;
f1=fn;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment