fork download
  1. public class Main {
  2. public static void main(String[] args) {
  3. System.out.println(fib(20));
  4. }
  5.  
  6. static int fib(int n) {
  7. String format = "%5d:%5d";
  8. int n1 = 1;
  9. int n2 = 1;
  10. System.out.println(String.format(format, 1, n1));
  11. System.out.println(String.format(format, 2, n2));
  12. for (int c = 3; c <= n; c++) {
  13. int n3 = n1 + n2;
  14. System.out.println(String.format(format, c, n3));
  15. n1 = n2;
  16. n2 = n3;
  17. }
  18. return n2;
  19. }
  20. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
    1:    1
    2:    1
    3:    2
    4:    3
    5:    5
    6:    8
    7:   13
    8:   21
    9:   34
   10:   55
   11:   89
   12:  144
   13:  233
   14:  377
   15:  610
   16:  987
   17: 1597
   18: 2584
   19: 4181
   20: 6765
6765