add 1
This commit is contained in:
parent
da9336ad09
commit
31b2e986bc
45
src/part5/aufg1.java
Normal file
45
src/part5/aufg1.java
Normal 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("==========");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user