Friday 10 April 2009

PROJECT EULER #7

Link to Project Euler problem 7

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 10001st prime number?

The second prime question. Again the Sieve of Eratosthenes makes the primes.


using System;
using System.Collections.Generic;

namespace ProjectEuler
{
class Program
{
static void Main(string[] args)
{
//Problem 7
DateTime start = DateTime.Now;
List<int> primes = new List<int>() { 2, 3 };
for (int i = 5; primes.Count < 10001; i += 2)
{
primes.Add(i);
foreach (int j in primes)
{
if (i % j == 0 && i != j)
{
primes.Remove(i);
break;
}
}
}
TimeSpan time = DateTime.Now-start;
Console.WriteLine("{0}\nThis took {1}",primes[10000] ,time);
Console.ReadKey();
}
}
}

No comments: