-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.fc
41 lines (39 loc) · 1.13 KB
/
5.fc
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
{-
TASK 5 - Fibonacci sequence
Implement a function that generates the Fibonacci
sequence from N to N+K terms (0<=N<=370; 0<=N+K<=371; 0<=K<=255).
The first two terms of the Fibonacci sequence are F_0 = 0 and F_1 = 1,
and the rest are defined as F_n = F_(n-1) + F_(n-2).
The resulting Fibonacci sequence should be stored in a tuple.
For example, a request with N = 1 and K = 3 should return a tuple [1, 1, 2],
and a request with N = 201 and K = 4 should return a tuple
[453973694165307953197296969697410619233826,
734544867157818093234908902110449296423351,
1188518561323126046432205871807859915657177,
1923063428480944139667114773918309212080528]
-}
() recv_internal() {
}
;; testable
(tuple) fibonacci_sequence (int n, int k) method_id {
tuple result = empty_tuple();
if ((n == 0) & (k > 0)) {
result~tpush(0);
}
if (((n == 1) & (k > 0)) | ((n == 0) & (k > 1))) {
result~tpush(1);
}
int i = 2;
int prev = 0;
int last = 1;
repeat (n + k - 2) {
int current = prev + last;
prev = last;
last = current;
if (i >= n) {
result~tpush(current);
}
i = i + 1;
}
return result;
}