-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci-sequence.linq
82 lines (71 loc) · 1.49 KB
/
fibonacci-sequence.linq
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<Query Kind="Program" />
void Main()
{
for(ulong i = 0; i < 20; i++)
{
Console.WriteLine(fib(i));
}
Console.WriteLine("---------------------------------------------");
Console.WriteLine("--\"Dynamic Progamming\"-----------------------");
for (ulong i = 0; i < 10000; i++)
{
Console.WriteLine(fib2(i));
}
Console.WriteLine("---------------------------------------------");
Console.WriteLine("--\"Dynamic Progamming2\"-----------------------");
var f = new Fibonacci();
for (ulong i = 0; i < 10000; i++)
{
Console.WriteLine(f.Calculate(i));
}
}
// O(2^N)
ulong fib(ulong i)
{
if(i==0) return 0;
if(i==1) return 1;
return fib(i - 1) + fib(i - 2);
}
// O(N)
// buffer values we already calculated instead of re-calculating each time
ulong fib2(ulong i)
{
ulong fib2(ulong i, ulong[] buf)
{
if (i == 0) return 0;
if (i == 1) return 1;
if (buf[i] == 0)
{
buf[i] = fib2(i - 1, buf) + fib2(i - 2, buf);
}
return buf[i];
}
ulong[] buf = new ulong[i+1];
return fib2(i, buf);
}
// cache calculated values in class
// for re-use on subsequent calls
class Fibonacci
{
ulong[] buf = new ulong[1000];
public ulong Calculate(ulong i)
{
if((i + 1) > (ulong)buf.LongLength)
{
ulong[] newbuf = new ulong[i * 2];
Array.Copy(this.buf, newbuf, this.buf.Length);
this.buf = newbuf;
}
return fib2(i);
}
private ulong fib2(ulong i)
{
if (i == 0) return 0;
if (i == 1) return 1;
if (buf[i] == 0)
{
buf[i] = fib2(i - 1) + fib2(i - 2);
}
return buf[i];
}
}