ecma-language-specification/bit-array/client.cs - A client of class BitArray. | Lecture 5 - slide 13 : 29 Program 5 |
// From the ECMA-334 C# Language specifications (section 17.8). // Also in Hejlsberg et al., The C# Programming Language, 2ed. using System; class CountPrimes { static int Count(int max) { BitArray flags = new BitArray(max + 1); int count = 1; for (int i = 2; i <= max; i++) { if (!flags[i]) { for (int j = i * 2; j <= max; j += i) flags[j] = true; count++; } } return count; } static void Main(string[] args) { int max = int.Parse(args[0]); int count = Count(max); Console.WriteLine("Found {0} primes between 1 and {1}", count, max); } }