public static void Main(string[] args) { // check that arguments have been supplied if (args.Length != 2) { Console.WriteLine("missing args"); System.Environment.Exit(1); } // attempt to open data file InFile data = new InFile(args[0]); if (data.OpenError()) { Console.WriteLine("cannot open " + args[0]); System.Environment.Exit(1); } // attempt to open results file OutFile results = new OutFile(args[1]); if (results.OpenError()) { Console.WriteLine("cannot open " + args[1]); System.Environment.Exit(1); } // various initializations int total = 0; IntSet mySet = new IntSet(); IntSet smallSet = new IntSet(1, 2, 3, 4, 5); string smallSetStr = smallSet.ToString(); // read and process data file int item = data.ReadInt(); while (!data.NoMoreData()) { total = total + item; if (item > 0) mySet.Incl(item); item = data.ReadInt(); } // write various results to output file results.Write("total = "); results.WriteLine(total, 5); results.WriteLine("unique positive numbers " + mySet.ToString()); results.WriteLine("union with " + smallSetStr + " = " + mySet.Union(smallSet).ToString()); results.WriteLine("intersection with " + smallSetStr + " = " + mySet.Intersection(smallSet).ToString()); results.Close(); }
public static void Main(string [] args) { const int Max = 32000; IntSet uncrossed = new IntSet(); // the sieve int i, n, k, it, iterations, primes = 0; // counters IO.Write("How many iterations? "); iterations = IO.ReadInt(); bool display = iterations == 1; IO.Write("Supply largest number to be tested "); n = IO.ReadInt(); if (n > Max) { IO.Write("n too large, sorry"); System.Environment.Exit(1); } IO.WriteLine("Prime numbers between 2 and " + n); IO.WriteLine("-----------------------------------"); for (it = 1; it <= iterations; it++) { primes = 0; for (i = 2; i <= n; i++) // clear sieve uncrossed.Incl(i); for (i = 2; i <= n; i++) // the passes over the sieve if (uncrossed.Contains(i)) { if (display && primes % 8 == 0) { // ensure line not too long IO.WriteLine(); } primes++; if (display) IO.Write(i, 6); k = i; // now cross out multiples of i do { uncrossed.Excl(k); k += i; } while (k <= n); } if (display) IO.WriteLine(); } IO.Write(primes + " primes"); }