Skip to main content
deleted 21 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I am new to C# programming. I wrote a little program in C# to calculate all primes until a user-given value. Ignoring Ignoring the lack of computing power I wrote this program to handle theoretically very huge numbers.

Can you give me suggestions to improve the code? Maybe even to make it faster?

Here's the code:

I am new to C# programming. I wrote a little program in C# to calculate all primes until a user-given value. Ignoring the lack of computing power I wrote this program to handle theoretically very huge numbers.

Can you give me suggestions to improve the code? Maybe even to make it faster?

Here's the code:

I am new to C# programming. I wrote a little program in C# to calculate all primes until a user-given value. Ignoring the lack of computing power I wrote this program to handle theoretically very huge numbers.

Can you give me suggestions to improve the code? Maybe even to make it faster?

Rollback to Revision 3
Source Link

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.


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.

Added improvement
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.


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.

Tweeted twitter.com/StackCodeReview/status/1027208102595190785
edited tags; edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Loading
Spelling fixes
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
Loading
Source Link
Kajkrow
  • 213
  • 1
  • 6
Loading