mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-06 23:03:58 +00:00
Initial commit
This commit is contained in:
parent
dd097904c2
commit
2b1f5c1254
48 changed files with 36395 additions and 0 deletions
36
vscode/Chapter11/LinqInParallel/Program.cs
Normal file
36
vscode/Chapter11/LinqInParallel/Program.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
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)
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue