-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacciSequence.java
59 lines (54 loc) · 1.69 KB
/
FibonacciSequence.java
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
// The Fibonacci numbers are generated by setting F0=0, F1=1, and then using the recursive formula
// F(n) = F(n-1) + F(n-2)
public class FibonacciSequence extends Sequence{
private
int current_element, next_element; // value of current element and the next element
int F0, F1;
public FibonacciSequence (int e1, int e2){
F0 = current_element = e1;
F1 = next_element = e2;
indx = 0;
}
public FibonacciSequence(){
F0 = current_element = 0;
F1 = next_element = 1;
indx = 0;
}
public int get_element(){
return current_element;
}
private void reset(){ // reset the sequence to initial state
current_element = F0;
next_element = F1;
indx = 0;
}
public int get_next_element(){
int val = next_element;
next_element = current_element + next_element;
current_element = val;
indx++;
return val;
}
public int get_prev_element(){
indx--;
int prev_element = next_element - current_element;
next_element = current_element;
current_element = prev_element;
return current_element;
}
public int get_element_by_index(int index){
if (indx >= index) {
index = Math.abs(index - indx);
while (index-- > 0) this.get_prev_element();
} else {
index = Math.abs(index - indx);
while (index-- > 0) this.get_next_element();
}
return this.get_element();
}
public int get_sum_of_elements(int start, int end) {
int sum = 0;
for (int i = start; i <= end; i += 1) sum += this.get_element_by_index(i);
return sum;
}
}