import java.util.Scanner;
import java.util.Locale;
public class FibTester {
public static void main(String[] args) { int n = 0; long time = 0; long fibNumber = 0;
if (args.length > 0) {try {n = Integer.parseInt(args[0]);} catch (NumberFormatException ecc1) {System.err.println("L'argomento non è un numero intero."); System.exit(1);}}
else { Scanner pustola = new Scanner(System.in); pustola.useLocale(Locale.US); System.out.println("Inserire un numero intero: ");
try {n = Integer.parseInt(pustola.next());}
catch (NumberFormatException ecc2)*{System.err.println("L'argomento non è un numero intero."); System.exit(1);} pustola.close();
}
System.out.println("\n\nCalcolo con algoritmo ricorsivo:");
time = System.currentTimeMillis();
fibNumber = recursiveFib(n);
time = System.currentTimeMillis() - time;
System.out.println("Risultato: " + fibNumber + "\nTempo impiegato: "+ time);
System.out.println("\n\nCalcolo con algoritmo iterativo:");
time = System.currentTimeMillis();
fibNumber = iterativeFib(n);
time = System.currentTimeMillis() - time;
System.out.println("Risultato: " + fibNumber + "\nTempo impiegato: "+ time);
}
public static long recursiveFib(int n) {
if (n < 0) {throw new IllegalArgumentException();}
if (n == 0) {return 0;} if (n == 1) {return 1;} if (n == 2)*{return 1;}
return (recursiveFib(n - 2) + recursiveFib(n - 1));
}
public static long iterativeFib(int n) { long f = 1; long fib1 = 1; long fib2 = 1;
if (n < 0) {throw new IllegalArgumentException();}
if (n == 0) {return 0;} if (n == 1) {return 1;} if (n == 2)*{return 1;}
for (int i = 3; i <= n; i++) {f = fib2 + fib1; fib1 = fib2; fib2 = f;}
return f;
}
}