sisibest

导航

求质数的算法

class Program
    {
        static void Main(string[] args)
        {
       //From 0 - Num
int num = 10000000; PrimeNumber(num); //PrimeNumber2(num); PrimeNumberWithoutTwo(num); //PrimeNumberWithoutTwo2(num); } private static void PrimeNumber(int num) { int count = 0; bool isPrime; for (int i = 2; i <= num; i++) { isPrime = true; for (int j = 2; j < i / j + 1; j++) //for (int j = 2; j < i; j++) { if (i % j == 0) isPrime = false; count++; } //if (isPrime) // Console.WriteLine(i); } Console.WriteLine("total count:{0:d}", count); } private static void PrimeNumberWithoutTwo(int num) { int count = 0; bool isPrime; if (num % 2 == 0) num = num - 1; for (int i = 3; i <= num; i = i + 2) { isPrime = true; for (int j = 3; j < i / j + 1; j = j + 2) //for (int j = 2; j < i; j++) { if (i % j == 0) isPrime = false; count++; } //if (isPrime) // Console.WriteLine(i); } Console.WriteLine("total count:{0:d}", count); } }

 

posted on 2013-11-22 10:56  sisibest  阅读(217)  评论(0编辑  收藏  举报