欧拉计划 第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 10 001st prime number?

 

class Program
    {
        private static List<long> primes = new List<long>(10000000);

        static void Main(string[] args)
        {
            primes.Add(2);
            for (int i = 0; primes.Count < 10001; i++)
            {
                NextPrime();
            }
            Console.WriteLine(primes.Last());
        }

        private static long NextPrime()
        {
            long last = primes.Last();
            long n = last + 1;
            while (true)
            {
                foreach (var item in primes)
                {
                    if (n % item == 0)
                    {
                        goto LoopOut;
                    }
                }
                primes.Add(n);
                return n;

            LoopOut:
                n++;
            }
        }
    }

 

posted @ 2011-08-28 17:30  kiminozo  阅读(227)  评论(0编辑  收藏  举报