Timur69

Фибоначи

Apr 4th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.46 KB | None | 0 0
  1. package tasks3;
  2.  
  3. public class Fibonacci {
  4.     public static int fib(int n) {
  5.         if (n <= 1) {
  6.             return n;
  7.         }
  8.         int previous = 0, next = 1, sum = 0;
  9.         for (int i = 1; i <= n; i++) {
  10.             previous = next;
  11.             next = previous + (sum - previous);
  12.             sum = previous + next;
  13.         }
  14.         return sum;
  15.     }
  16.  
  17.     public static void main(String[] args) {
  18.         System.out.println(fib(3));
  19.     }
  20. }
Add Comment
Please, Sign In to add comment