DEV/DEV3.2/TP2/FibonacciRapide.java
2024-10-17 12:28:41 +02:00

40 lines
1.0 KiB
Java

import java.util.HashMap;
public class FibonacciRapide {
private static int indentation;
private static HashMap<Long, Long> buffer = new HashMap<>();
public static long fibo(long indice) {
if (indice == 0) {
return 0;
} else if (indice == 1) {
return 1;
} else {
if (buffer.containsKey(indice)) {
return buffer.get(indice);
}
for (int i = 0; i < indentation; i++) {
System.out.print(" ");
}
System.out.println("Terme n°"+indice);
indentation++;
long result = fibo(indice - 1) + fibo(indice - 2);
buffer.put(indice, result);
for (int i = 0; i < indentation-1; i++) {
System.out.print(" ");
}
indentation--;
System.out.println("Résultat du calcul: "+result);
return result;
}
}
public static void main(String args[]) {
long indicemax = Long.parseUnsignedLong(args[0]);
indentation=0;
System.out.println("Le terme n°" + indicemax + " de la suite de Fibonacci est égal à " + fibo(indicemax));
}
}