Euler 7 Trivial with LINQ and Generators

Date Published: 04 September 2009

Euler 7 Trivial with LINQ and Generators

Euler problem 7 requires returning the 10001st prime number. It notes that the 6th prime number is 13 in the problem description. Having already done some work with iterators and various number generators, including a Primes generator for previous Euler problems, the base case given in the problem can be reduced to this NUnit test:

<span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SixthPrimeIs13()
{
    Assert.AreEqual(13, NumberGenerator.Primes().Take(6).Last());
}

Replacing the 6 with 10001 takes care of the rest. Iā€™m really starting to dig using LINQ for this stuff.

Steve Smith

About Ardalis

Software Engineer

Steve is an experienced software architect and trainer, focusing currently on ASP.NET Core and Domain-Driven Design.


Ardalis

Copyright Ā© 2020