Skip to content

Commit 99c7f9b

Browse files
authored
Merge pull request #40 from DanexCodr/copilot/add-linear-recurrence-closed-form
Make linear-recurrence array access O(1) / amortized O(1)
2 parents c3fe7fc + f5761a9 commit 99c7f9b

4 files changed

Lines changed: 669 additions & 2 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
unit test
2+
3+
share main() {
4+
out("=== Linear recurrence optimization ===")
5+
start := timer()
6+
7+
fib := [0 to 2000]
8+
fib[0] = 0
9+
fib[1] = 1
10+
for i of [2 to 2000] {
11+
fib[i] = fib[i-1] + fib[i-2]
12+
}
13+
out("fib-metadata=" + fib)
14+
out("fib[10]=" + fib[10] + " expected=55")
15+
out("fib[20]=" + fib[20] + " expected=6765")
16+
out("fib[30]=" + fib[30] + " expected=832040")
17+
18+
jac := [0 to 2000]
19+
jac[0] = 0
20+
jac[1] = 1
21+
for i of [2 to 2000] {
22+
jac[i] = jac[i-1] + 2 * jac[i-2]
23+
}
24+
out("jac[10]=" + jac[10] + " expected=341")
25+
out("jac[20]=" + jac[20] + " expected=349525")
26+
27+
shift := [0 to 2000]
28+
shift[0] = 0
29+
shift[1] = 1
30+
for i of [2 to 2000] {
31+
shift[i] = shift[i-1] + shift[i-2] + 5
32+
}
33+
out("shift[2]=" + shift[2] + " expected=6")
34+
out("shift[3]=" + shift[3] + " expected=12")
35+
out("shift[10]=" + shift[10] + " expected=495")
36+
out("elapsed_ms=" + (timer() - start))
37+
}

0 commit comments

Comments
 (0)