-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart2.java
More file actions
61 lines (52 loc) · 1.56 KB
/
Part2.java
File metadata and controls
61 lines (52 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Scanner;
public class Part2 {
static int[] storage = new int[100];
static int c1 = 1;
static int c2 = 1;
static int i = 1;
static int number = 1;
/*pseudocode
1. Start with 2 givens, in this case we start with 2 as our first value
and 3 as our second value.
2. Function will add the adjacent indexes up
2. recursively call function until we reach that nth value we are going
for
*/
public static void main(String[] str){
Scanner input = new Scanner(System.in);
System.out.print("Which F number do you need?: ");
FibonacciNoStorage(input.nextInt());
}
/**
* Recursive function that returns the nth fibonacci number
* @param F the fibonacci number F(n)
*/
public static void Fibonacci(int F){
int number=1;
storage[0]=1;
storage[1]=1;
storage[i] = storage[i-1]+ storage[i-2];
number = storage[i-1];
if(i==F){
System.out.println(number);
}
else{
i++;
Fibonacci(F);
}
}
//Fibonacci sequence calculator using only static variables
//Reduces amount of memory used
public static void FibonacciNoStorage(int F){
number = c2+c1;
c1=c2;
c2 = number;
if(i == F){
System.out.println(number -c1);
}
else{
i++;
FibonacciNoStorage(F);
}
}
}