Skip to main content
Post Made Community Wiki by Jamal
added 79 characters in body
Source Link
Kajkrow
  • 213
  • 1
  • 6

After lots and lots of input (Thank you all!) I created this solution now. It This is a improved code, that has a different approach to the problem. This code is limited to the first 2.000.000 numbers:

After lots and lots of input (Thank you all!) I created this solution now. It is limited to the first 2.000.000 numbers:

After lots and lots of input (Thank you all!) I created this solution now. This is a improved code, that has a different approach to the problem. This code is limited to the first 2.000.000 numbers:

Source Link
Kajkrow
  • 213
  • 1
  • 6

After lots and lots of input (Thank you all!) I created this solution now. It is limited to the first 2.000.000 numbers:

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Diagnostics;

namespace ConsolePrimes
{
    public static class Program
    {
        public static bool[] nonprimes = new bool[2000001];
        public static ulong i = 2;
        public static ulong inputnumber = 0;
        public static void Main(string[] args)
        {
            bool repeat = true;
            while (repeat)
            {
                Console.Clear();
                //Console.WriteLine("Up to which number shall all primes be calculated?");
                Console.WriteLine("Calculation until 2.000.000...");
                //string inputvar = Console.ReadLine();
                string inputvar = "2000000";
                Console.ReadLine();
                Stopwatch stopWatch = Stopwatch.StartNew();
                //inputnumber = Convert.ToUInt64(inputvar);
                inputnumber = 2000000;
                nonprimes[0] = true;
                nonprimes[1] = true;
                calcprime();
                i++;
                while ( i < inputnumber )
                {
                    calcprime();
                    i += 2;
                }
                List<ulong> primeoutput = new List<ulong>(1000000);
                ulong counter = 0;
                foreach (bool element in nonprimes)
                {
                    if (!(element))
                    {
                        primeoutput.Add(counter);
                    }
                    counter++;
                }
                stopWatch.Stop();
                Console.WriteLine("The Range from 0 to " + inputvar + " has " + Convert.ToString(primeoutput.Count) + " primes.");
                Console.WriteLine("The calculation took "+  Convert.ToString(stopWatch.Elapsed.TotalMilliseconds)  + "ms to be finished.");
                Console.WriteLine("The list of all primes is now getting exported to \"primes.txt\".");
                TextWriter tw = new StreamWriter("primes.txt");
                foreach (ulong nr in primeoutput)
                {
                   tw.WriteLine(nr);
                }
                tw.Close();
                Console.WriteLine("\nDo you want to start again?");
                string userinput = Console.ReadLine();
                string lowercaseinput = userinput.ToLower();
                if ( !(lowercaseinput.Equals("y")) )
                {
                    repeat = false;
                }
            }
        }
        public static void calcprime()
        {
            ulong k = 2;
            ulong j = i;
            ulong l = 0;
            while ( j*k <= inputnumber)
            {
                l = j*k;
                nonprimes[l] = true;
                k++;
            }
        }
    }
}

Now it takes around 50ms to calculate all primes until 2.000.000, previously it took over 20s to calculate until 1.000.000. So this is a huge improvement.

ATM I can only calculate primes until barely under 2.5 million. I will work on improving the code to go further.