【ProjectEuler】ProjectEuler_007

// 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?

using System;
using System.Collections.Generic;
using System.Text;

namespace projecteuler007
{
    class Program
    {
        static void Main(string[] args)
        {
            F1();
        }

        private static void F1()
        {
            Console.WriteLine(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod());
            DateTime timeStart = DateTime.Now;

            int i = 0;
            long j = 1;
            int findIndex = 10001;
            while (true)
            {
                if (isPrime(++j))
                {
                    i++;
                   // Console.WriteLine(i + " " + j);
                    if (i == findIndex)
                    {
                        Console.WriteLine(j);
                        break;
                    }
                }
            }

            Console.WriteLine("Total Milliseconds is " + DateTime.Now.Subtract(timeStart).TotalMilliseconds + "\n\n");
        }

        /// <summary>
        /// 判断是否为素数
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        private static bool isPrime(long n)
        {
            if ((n % 2 == 0 && n != 2) || n <= 1)
            {
                return false;
            }

            long sqrtValue = long.Parse(Math.Sqrt(n).ToString().Split('.')[0]);

            for (int i = 3; i <= sqrtValue; i += 2)
            {
                if (n % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}

/*
Void F1()
104743
Total Milliseconds is 116.5148

By GodMoon
*/

你可能感兴趣的:(【ProjectEuler】ProjectEuler_007)