Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // vet_reference_primes_up_to_64k.linq
- // 2016-04-29
- //
- // https://primes.utm.edu/lists/small/10000.txt
- // http://pari.math.u-bordeaux.fr/gp.html
- // forprime(p = 2, 2^16, print(p))
- // primes(6542)
- //
- // see http://codereview.stackexchange.com/q/127024/56653
- const string ReferenceFile = "reference_primes_up_to_64k.txt"; // <-- file to be vetted
- const string First10K = "d:/dev/cpp/algo/primes/common/10000.txt"; // <-- downloaded file vetted against
- void Main ()
- {
- var primes = System.IO.File.ReadAllLines(ReferenceFile).Select(ushort.Parse).ToList();
- assert_equal("count", 6542, primes.Count);
- var sum = primes.Select((p) => (int)p).Sum();
- assert_equal("sum", 202288087, sum);
- var primorial_64bit = primes.Select((p) => (ulong)p).Aggregate((product, prime) => product * prime);
- assert_equal("product", 8987519195527561682UL, primorial_64bit);
- if (!primes.SequenceEqual(read_primes_up_to_64K_from_utm_edu_file()))
- throw new Exception("!primes.SequenceEqual(read_primes_up_to_64K_from_utm_edu_file())");
- Console.Write("{0} vetted successfully", ReferenceFile);
- var current_attributes = System.IO.File.GetAttributes(ReferenceFile);
- if ((current_attributes & FileAttributes.ReadOnly) == 0)
- {
- Console.Write(" - making the file read-only");
- System.IO.File.SetAttributes(ReferenceFile, current_attributes | FileAttributes.ReadOnly);
- }
- }
- static void assert_equal<T> (string what, T expected, T value)
- {
- if (!EqualityComparer<T>.Default.Equals(value, expected))
- throw new Exception(string.Format("{0}: expected {1}, got {2}", what, expected, value));
- }
- static List<ushort> read_primes_up_to_64K_from_utm_edu_file (string filename = First10K)
- {
- var result = new List<ushort>();
- foreach (var line in System.IO.File.ReadAllLines(filename).Select((s) => s.Trim()))
- {
- if (line.Length > 0 && char.IsDigit(line[0]))
- {
- var numbers = line.Split(' ')
- .Where((x) => x.Length > 0)
- .Select(int.Parse)
- .Where((x) => x <= ushort.MaxValue)
- .Select((x) => (ushort)x);
- result.AddRange(numbers);
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement