Beispiel #1
0
        static void Main()
        {
            using var calculator = new CollatzCalculator(ConfigurationManager.ConnectionStrings["Collatz"].ConnectionString);

            Console.Write("Calculate up to? ");

            var upper = BigInteger.Parse(Console.ReadLine());

            (BigInteger seed, BigInteger count)longest = (0, 0);

            //Parallel.For(0, upper, seed =>
            for (BigInteger seed = 0; seed < upper; ++seed)
            {
                var count = calculator.GetCount(seed);

                if (count > longest.count)
                {
                    longest = (seed, count);
                }

                calculator.SaveChanges();
            }//);

            foreach (var(value, count) in calculator.Sequence(longest.seed))
            {
                Console.WriteLine($"{count:N0}: {value:N0}");
            }

            Console.WriteLine($"Longest sequence with a seed up to {upper:N0} is for seed of {longest.seed:N0} with {longest.count:N0} elements.");
            //    Console.WriteLine($"{calculator.AllocationCount:N0} numbers calculated with a peak value of {calculator.PeakValue:N0}.");
        }
Beispiel #2
0
        static void Main()
        {
            try
            {
                Console.Write("Calculate up to? ");

                int upper = int.Parse(Console.ReadLine());

                CollatzCalculator calculator = new CollatzCalculator(0X7FEFFFFF);

                calculator.Generate();

                (int count, int seed)longest = (0, 0);

                for (int seed = 1; seed < upper; ++seed)
                {
                    try
                    {
                        int count = calculator.Sequence(seed).Count();

                        if (count > longest.count)
                        {
                            longest = (count, seed);
                        }
                    }
                    catch (IndexOutOfRangeException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }

                foreach (var(count, value) in calculator.Sequence(longest.seed))
                {
                    Console.WriteLine($"{count}: {value}");
                }

                Console.WriteLine($"Longest sequence with a seed up to {upper} is for seed of {longest.seed} with {longest.count} elements.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #3
0
        static void Main()
        {
            try
            {
                Console.Write("Calculate up to? ");

                long upper = long.Parse(Console.ReadLine());

                CollatzCalculator calculator = new CollatzCalculator();

                // Key is a particular peak value. Value is the number of sequences which have that peak value.
                ConcurrentDictionary <ulong, ulong> peakFrequency = new ConcurrentDictionary <ulong, ulong>();

                SequenceItem longest = new SequenceItem(0);     // Item for head of sequence with longest count
                SequenceItem highest = new SequenceItem(0);     // Item for head of sequence with highest peak

                object locker = new object();

                Parallel.For(1, upper, seed =>
                {
                    var entry = calculator.GetEntry((ulong)seed);

                    peakFrequency.AddOrUpdate(entry.Peak, 1, (k, v) => v + 1); // Incremen count for this peak

                    lock (locker)
                    {
                        // Check and update longest
                        if (entry.Count > longest.Count)
                        {
                            longest = entry;
                        }

                        // Check and update highest peak. In the event of a tie, choose the shortest sequence
                        if (entry.Peak > highest.Peak || entry.Peak == highest.Peak && entry.Count < highest.Count)
                        {
                            highest = entry;
                        }
                    }
                });

                Display(longest, "Longest sequence");

                Console.WriteLine();

                Display(highest, "Highest peak");

                Console.WriteLine();

                var mostFrequent = peakFrequency.OrderByDescending(p => p.Value).Take(3);

                Console.WriteLine("Most common peaks (top 3):");

                foreach (var pair in mostFrequent)
                {
                    Console.WriteLine($"   {pair.Key:N0}  ({pair.Value:N0} occurrences).");
                }

                Console.WriteLine();

                Console.WriteLine($"{calculator.AllocationCount:N0} numbers calculated with a peak value of {calculator.PeakValue:N0}.");

                // Helper to display a full sequence
                void Display(SequenceItem item, string label)
                {
                    Console.WriteLine($"{label} with a seed up to {upper:N0} is for seed of {item.Current:N0} with {item.Count:N0} elements\n\tand with a peak value of {item.Peak:N0}.");

                    foreach (var entry in calculator.Sequence(item.Current))
                    {
                        Console.WriteLine($"{entry.Count:N0}: {entry.Current:N0} {(item.Peak == entry.Current ? "-- PEAK --" : "")}");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }