This commit is contained in:
Matti 2024-04-11 15:34:32 +02:00
parent da9336ad09
commit 31b2e986bc

45
src/part5/aufg1.java Normal file
View File

@ -0,0 +1,45 @@
package part5;
public class aufg1 {
public static int fibonacciIter(int number){
int smol = 0;
int big = 1;
int temp;
for (int i=0; i<number; i++){
temp = smol;
smol = big;
big = smol + temp;
}
return big;
}
public static int fibonacciRec(int number){
int result;
if(number < 0){ // Output 0 instead of Error
result = 0;
} else if (number < 2) {
result = 1;
}else if (number == 10) { // Optimization1
result = 89;
}else if (number == 20) { // Optimization2
result = 10946;
}else if (number == 30) { // Optimization3
result = 1346269;
}else {
result = fibonacciRec(number-1) + fibonacciRec(number-2);
}
return result;
}
public static void main(String[] agrs){
// Fib(47) causes an Integer Overflow
for (int i=0; i<46; i++) {
System.out.println(fibonacciIter(i));
System.out.println(fibonacciRec(i));
System.out.println("==========");
}
}
}