static void Main(string[] args) { // Hi Steven. // Hi Tariq and Murali; oh, and hello World! ///////////// FOR PRIMES //////////////////// // Ask user how many primes they want. System.Console.WriteLine("Please input the number you want to check up to. (Must be above 3.)"); int prime = System.Convert.ToInt32(System.Console.ReadLine()); // For regular for loop Prime Generator. //PrimeGenerator gen = new PrimeGenerator(); //gen.PrintPrimes(prime); // For Sieve Algorithm Generator SieveGenerator sieve = new SieveGenerator(); sieve.PrintPrimes(prime); // Prime MUST be greater than 3 for this to work. ////////////////////////////////////////////////////////// /////////////// FOR DIGITS ///////////////// System.Console.WriteLine("Please enter a number so we can break it down into its digits."); int digits = System.Convert.ToInt32(System.Console.ReadLine()); NumberOps num = new NumberOps(); num.PrintDigits(digits); /////////////////////////////////////////////////////// }
public static void Main(string [] args) { IPrimeGenerator pg; // pg = new PrimeGenerator(); // FIXME: this is a hack since we iterate to the // first prime ABOVE 2000000. pg = new SieveGenerator(2010000); pg.Init(); UInt32 current = pg.Next(); UInt64 sum = 0; int count = 1; for (; current < 2000000 ; current = pg.Next(), count++) { if (UInt64.MaxValue - sum < current) throw new Exception("OVERFLOW"); sum += current; if (count % 100 == 0) Console.WriteLine("{0}:{1}...", count, current); } Console.WriteLine("Result: {0}", sum); }
public void SieveGTest() { SieveGenerator gen = new SieveGenerator(); //////////////////////////////////////////////////////////// // Check that the UnmarkedList is correct. int n = 100; // This list should contain the numbers from 2 to 100. int[] UnmarkedArray = gen.UnmarkedList(n).Select(UnmarkedList => UnmarkedList.number).ToArray(); // Jesus christ looking up that syntax to convert a list of objects to an array was crazy. for (int k = 2; k < n; k++) { if (k != UnmarkedArray[k - 2]) { throw new Exception("UnmarkedList gave a bad list in SieveGenerator.cs"); } } ////////////////////////////////////////////////////////////// // Check if GetPrimes works. int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19 }; int[] notprimes = { 0, 1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18 }; // Generate a list of primes from GetPrimes() in PrimeGenerator.cs. // The list will be up to the number of manual entries we have for our unit test. List <int> TestList = gen.GetPrimes(primes.Length + notprimes.Length); for (int w = 0; w < primes.Length; w++) { // Check our manually entered prime list compared to our generated list. if (primes[w] != TestList[w]) { throw new Exception("GetPrimes in PrimeGenerator.cs includes a nonprime number in the list."); } } //////////////////////////////////////////////////////////// }
public static void Main(string[] args) { // IPrimeGenerator generator = new BruteForceGenerator(); // FIXME: Hack. 1010000 used to ensure that we get a prime above // 1000000. IPrimeGenerator generator = new SieveGenerator(1010000); generator.Init(); UInt32 p = generator.Next(); // FIXME: checking existence in this hashset takes way too long... HashSet<UInt32> circularPrimes = new HashSet<UInt32>(); while(p <= 1e6) { // Console.WriteLine("Trying {0}", p); List<UInt32> rotations = findRotations(p); if ((p == 2 || rotations.All(i => i % 2 != 0)) && // filter even except for two rotations.All(target => generator.IsPrime(target)) && !circularPrimes.Contains(p)) { circularPrimes.UnionWith(rotations); Console.WriteLine("circular set: [{0}]", String.Join(", ", rotations.Select(i => i.ToString()).ToArray())); } p = generator.Next(); } Console.WriteLine("Result: {0}", circularPrimes.Count); }