mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
37 lines
835 B
C#
37 lines
835 B
C#
using System.Diagnostics; // Stopwatch
|
|
|
|
Write("Press ENTER to start. "); ReadLine();
|
|
Stopwatch watch = Stopwatch.StartNew();
|
|
|
|
int max = 45;
|
|
IEnumerable<int> numbers = Enumerable.Range(start: 1, count: max);
|
|
|
|
WriteLine($"Calculating Fibonacci sequence up to term {max}. Please wait...");
|
|
|
|
// int[] fibonacciNumbers = numbers
|
|
// .Select(number => Fibonacci(number))
|
|
// .ToArray();
|
|
|
|
int[] fibonacciNumbers = numbers.AsParallel()
|
|
.Select(number => Fibonacci(number))
|
|
.OrderBy(number => number)
|
|
.ToArray();
|
|
|
|
watch.Stop();
|
|
WriteLine("{0:#,##0} elapsed milliseconds.",
|
|
arg0: watch.ElapsedMilliseconds);
|
|
|
|
Write("Results:");
|
|
foreach (int number in fibonacciNumbers)
|
|
{
|
|
Write($" {number:N0}");
|
|
}
|
|
|
|
static int Fibonacci(int term) =>
|
|
term switch
|
|
{
|
|
1 => 0,
|
|
2 => 1,
|
|
_ => Fibonacci(term - 1) + Fibonacci(term - 2)
|
|
};
|