求质数的算法
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); } }