Exemple #1
0
        public static void StartSieve(BigInteger max, CancellationToken?token)
        {
            var help = Max;

            Max = max;
            CustomConsole.WriteLine($"Calculating Primes till {Max} with 1 Thread and PrimeSieve...");
            var watch = new Stopwatch();

            watch.Start();
            var primes = StaticPrime.PrimeSieve((int)Max, token);

            watch.Stop();
            var elapsed = watch.Elapsed.ToString();

            CustomConsole.WriteLine(
                $"Calculation finished in {double.Parse(elapsed.Substring(elapsed.LastIndexOf(":", StringComparison.Ordinal) + 1))} Seconds.");
            CustomConsole.NewLine();

            watch.Reset();

            CustomConsole.WriteLine("Writing to file...");
            watch.Start();
            FileHelper.Restart();
            FileHelper.WriteFile(primes);
            FileHelper.Dispose();
            watch.Stop();
            CustomConsole.WriteLine($"Finished in {watch.Elapsed.ToString()}.");
            CustomConsole.NewLine();
            Max = help;
        }
Exemple #2
0
        public static void Start(CancellationToken?token)
        {
            var watch = new Stopwatch();


            CustomConsole.ReplaceLine("Picking up, where we left off...");
            var lastPrime = FileHelper.FindLastPrime();

            if (lastPrime >= Max - 100)
            {
                CustomConsole.ReplaceLine($"Already found {lastPrime}.");
                return;
            }

            CustomConsole.ReplaceLine($"Starting at {lastPrime}.");

            if (lastPrime < MaxSieveValue)
            {
                StartSieve(MaxSieveValue >= Max ? Max : MaxSieveValue, token);
                lastPrime = FileHelper.FindLastPrime();
                if (lastPrime >= Max - 100)
                {
                    return;
                }
            }

            System.Threading.Thread.Sleep(1000);

            CustomConsole.WriteLine("Starting calculation...");
            watch.Start();

            var current = lastPrime;

            for (BigInteger i = 0;
                 current < Max && !(token?.IsCancellationRequested ?? false);
                 current = lastPrime + ++i * Step)
            {
                CustomConsole.ReplaceLine($"Calculating till {current + Step}...");
                var bag = new ConcurrentBag <BigInteger>();

                ParallelFor(current, current + Step, n =>
                {
                    if (StaticPrime.IsPrime(n))
                    {
                        bag.Add(n);
                    }
                });

                var list = bag.ToList();
                list.Sort();
                FileHelper.WriteFile(list);
            }

            watch.Stop();
            FileHelper.Dispose();
            CustomConsole.NewLine();
            CustomConsole.WriteLine(
                $"Calculated for: {watch.Elapsed.ToString().Substring(0, watch.Elapsed.ToString().LastIndexOf('.'))}. Biggest Prime found: {FileHelper.FindLastPrime()}"
                );
        }