/
FibonacciIterative.cs
50 lines (42 loc) · 1.16 KB
/
FibonacciIterative.cs
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
namespace Fibonacci
{
using System;
using System.Collections.Generic;
using System.Numerics;
public class FibonacciIterative : IFibonacci
{
public BigInteger GetFibonacci(int n)
{
if (n < 0)
{
throw new ArgumentException($"{nameof(n)} must be greater than or equal to 0.");
}
if (n <= 1)
{
return n;
}
BigInteger current = 0;
BigInteger next = 1;
for (int i = 0; i < n; i++)
{
BigInteger temp = current;
current = next;
next += temp;
}
return current;
}
public List<BigInteger> GetFibonacciSequence(int length)
{
if (length < 0)
{
throw new ArgumentException($"{nameof(length)} must be greater than or equal to 0.");
}
var sequence = new List<BigInteger>();
for (int i = 0; i < length; i++)
{
sequence.Add(GetFibonacci(i));
}
return sequence;
}
}
}