Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tasks3;
- public class Fibonacci {
- public static int fib(int n) {
- if (n <= 1) {
- return n;
- }
- int previous = 0, next = 1, sum = 0;
- for (int i = 1; i <= n; i++) {
- previous = next;
- next = previous + (sum - previous);
- sum = previous + next;
- }
- return sum;
- }
- public static void main(String[] args) {
- System.out.println(fib(3));
- }
- }
Add Comment
Please, Sign In to add comment