Skip to content

Commit fab47ec

Browse files
committed
Added C# implementation of Fibonacci
1 parent 4e34d8f commit fab47ec

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections.Generic;
2+
3+
namespace Fib
4+
{
5+
public static class Fibonacci
6+
{
7+
/// <summary>
8+
/// Generator method that calculates the Fibonacci sequence
9+
/// </summary>
10+
/// <returns>The fibonacci sequence</returns>
11+
public static IEnumerable<ulong> Sequence()
12+
{
13+
ulong a = 1;
14+
ulong b = 1;
15+
16+
yield return a;
17+
yield return b;
18+
19+
while (true)
20+
{
21+
yield return a + b;
22+
23+
ulong temp = a;
24+
a = b;
25+
b = temp;
26+
a += b;
27+
}
28+
}
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Fib
8+
{
9+
public static class Fibonacci_test
10+
{
11+
static void Main(string[] args)
12+
{
13+
Test();
14+
Console.ReadKey();
15+
}
16+
17+
public static void Test()
18+
{
19+
foreach (var numb in Fibonacci.Sequence())
20+
{
21+
Console.WriteLine(numb);
22+
23+
if (Console.ReadKey(true).Key == ConsoleKey.Escape)
24+
break;
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)