fibonacci/Fibo.java

21 lines
635 B
Java
Raw Normal View History

2022-04-29 10:32:32 +03:00
// Fibonacci number generator in Java using recursion
2022-04-29 10:25:41 +03:00
public class Fibo
{
2022-04-29 10:32:32 +03:00
static final int COUNT = 40;
2022-04-29 10:25:41 +03:00
private static long fibo(int i){
if (i < 3)
return 1;
else
return fibo(i-1) + fibo(i-2);
}
public static void main(String[] args) {
2022-04-29 10:32:32 +03:00
System.out.println("Lasketaan fibo: " + COUNT);
2022-04-29 10:25:41 +03:00
long before = System.currentTimeMillis();
2022-04-29 10:32:32 +03:00
for (int i=1; i <= COUNT; i++)
2022-04-29 10:25:41 +03:00
System.out.println("Fibo: " + i + " = " + fibo(i));
long after = System.currentTimeMillis();
System.out.println("Laskenta kesti: " + (after - before) + " ms");
}
}