Last active
February 5, 2017 15:52
-
-
Save maxint137/c3ea40cfa133c772502e4f2bdf6e33e4 to your computer and use it in GitHub Desktop.
Project Euler #7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13 | |
// What is the 10 001st prime number? | |
// https://projecteuler.net/problem=7 | |
void Main() | |
{ | |
var knownPrimes = new List<int>(new int[] { 2 }); | |
int N = 3; | |
while (knownPrimes.Count() < 10001) | |
{ | |
if (isPrime(N, knownPrimes)) | |
{ | |
knownPrimes.Add(N); | |
} | |
N++; | |
} | |
knownPrimes.Last().Dump("Prime #" + knownPrimes.Count()); | |
} | |
bool isPrime(int n, IEnumerable<int> knownPrimes) | |
{ | |
if (Math.Sqrt(n) <= knownPrimes.Last()) | |
{ | |
return false == knownPrimes.Any(p=> 0 == n%p); | |
} | |
else | |
{ | |
throw new ApplicationException("Something wrong with knownPrimes"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment