Commit 98667cbf by Patryk Czarnik

fibonacci w pętli

parent f27e98f4
package domowe.r2.z03_funkcje;
import java.util.Scanner;
public class Fib2 {
static long fib(int n) {
long wynik = 0;
long poprz = 1;
for(int i = 0; i < n; i++) {
long nowy = wynik + poprz;
poprz = wynik;
wynik = nowy;
}
return wynik;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.print("Podaj argument: ");
int n = scanner.nextInt();
if(n < 0)
break;
long p = System.nanoTime();
var wynik = fib(n);
long k = System.nanoTime();
System.out.printf("F(%d) = %d\n", n, wynik);
System.out.printf(" czas: %.6f s\n", (k-p)*1e-9);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment