Project Euler: Find 10.001st prime

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I have just started learning C# and decided to do some problems from Project Euler. I wrote a code to find 10 001st prime and I thought it would be cool optimize it to make it as fast as possible. How could I improve this to make it even faster?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace primes

class Program

static void Main(string args)

long prime = 2;
long largestprime = 2;
long potentialprime = 3;
Console.WriteLine("Enter the prime you want to find: ");
long primenum = Convert.ToInt64(Console.ReadLine());
long primearray = new long[primenum + 1];
primearray[1] = 2;
Stopwatch sw = new Stopwatch();
sw.Start();
while (prime <= primenum)

bool isprime = true;
for (long x = 1; x < prime; x += 3)

if (isprime)

primearray[prime] = potentialprime;
prime += 1;
largestprime = potentialprime;

potentialprime += 2;

sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds + "ms to find ");
Console.WriteLine(largestprime);
Console.ReadKey();









share|improve this question

















  • 1




    You might want to take a look at the answers of my question.
    – Denis
    Jul 18 at 8:06






  • 2




    Shouldn't this code calculate the 10.001st prime? I don't think it does.
    – t3chb0t
    Jul 18 at 11:03










  • I changed it so it can calculate nth number prime where n is taken as an input
    – Naia Suzuki
    Jul 18 at 11:28






  • 1




    I would focus on increasing readability, maybe you will then spot the answer to your question by yourself.
    – Slampen
    Jul 19 at 7:16






  • 1




    This is Project Euler Problem 7 for those who want the link.
    – rossum
    Jul 29 at 12:19
















up vote
2
down vote

favorite












I have just started learning C# and decided to do some problems from Project Euler. I wrote a code to find 10 001st prime and I thought it would be cool optimize it to make it as fast as possible. How could I improve this to make it even faster?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace primes

class Program

static void Main(string args)

long prime = 2;
long largestprime = 2;
long potentialprime = 3;
Console.WriteLine("Enter the prime you want to find: ");
long primenum = Convert.ToInt64(Console.ReadLine());
long primearray = new long[primenum + 1];
primearray[1] = 2;
Stopwatch sw = new Stopwatch();
sw.Start();
while (prime <= primenum)

bool isprime = true;
for (long x = 1; x < prime; x += 3)

if (isprime)

primearray[prime] = potentialprime;
prime += 1;
largestprime = potentialprime;

potentialprime += 2;

sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds + "ms to find ");
Console.WriteLine(largestprime);
Console.ReadKey();









share|improve this question

















  • 1




    You might want to take a look at the answers of my question.
    – Denis
    Jul 18 at 8:06






  • 2




    Shouldn't this code calculate the 10.001st prime? I don't think it does.
    – t3chb0t
    Jul 18 at 11:03










  • I changed it so it can calculate nth number prime where n is taken as an input
    – Naia Suzuki
    Jul 18 at 11:28






  • 1




    I would focus on increasing readability, maybe you will then spot the answer to your question by yourself.
    – Slampen
    Jul 19 at 7:16






  • 1




    This is Project Euler Problem 7 for those who want the link.
    – rossum
    Jul 29 at 12:19












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have just started learning C# and decided to do some problems from Project Euler. I wrote a code to find 10 001st prime and I thought it would be cool optimize it to make it as fast as possible. How could I improve this to make it even faster?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace primes

class Program

static void Main(string args)

long prime = 2;
long largestprime = 2;
long potentialprime = 3;
Console.WriteLine("Enter the prime you want to find: ");
long primenum = Convert.ToInt64(Console.ReadLine());
long primearray = new long[primenum + 1];
primearray[1] = 2;
Stopwatch sw = new Stopwatch();
sw.Start();
while (prime <= primenum)

bool isprime = true;
for (long x = 1; x < prime; x += 3)

if (isprime)

primearray[prime] = potentialprime;
prime += 1;
largestprime = potentialprime;

potentialprime += 2;

sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds + "ms to find ");
Console.WriteLine(largestprime);
Console.ReadKey();









share|improve this question













I have just started learning C# and decided to do some problems from Project Euler. I wrote a code to find 10 001st prime and I thought it would be cool optimize it to make it as fast as possible. How could I improve this to make it even faster?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace primes

class Program

static void Main(string args)

long prime = 2;
long largestprime = 2;
long potentialprime = 3;
Console.WriteLine("Enter the prime you want to find: ");
long primenum = Convert.ToInt64(Console.ReadLine());
long primearray = new long[primenum + 1];
primearray[1] = 2;
Stopwatch sw = new Stopwatch();
sw.Start();
while (prime <= primenum)

bool isprime = true;
for (long x = 1; x < prime; x += 3)

if (isprime)

primearray[prime] = potentialprime;
prime += 1;
largestprime = potentialprime;

potentialprime += 2;

sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds + "ms to find ");
Console.WriteLine(largestprime);
Console.ReadKey();











share|improve this question












share|improve this question




share|improve this question








edited Jul 18 at 8:19









t3chb0t

31.8k54095




31.8k54095









asked Jul 18 at 7:42









Naia Suzuki

163




163







  • 1




    You might want to take a look at the answers of my question.
    – Denis
    Jul 18 at 8:06






  • 2




    Shouldn't this code calculate the 10.001st prime? I don't think it does.
    – t3chb0t
    Jul 18 at 11:03










  • I changed it so it can calculate nth number prime where n is taken as an input
    – Naia Suzuki
    Jul 18 at 11:28






  • 1




    I would focus on increasing readability, maybe you will then spot the answer to your question by yourself.
    – Slampen
    Jul 19 at 7:16






  • 1




    This is Project Euler Problem 7 for those who want the link.
    – rossum
    Jul 29 at 12:19












  • 1




    You might want to take a look at the answers of my question.
    – Denis
    Jul 18 at 8:06






  • 2




    Shouldn't this code calculate the 10.001st prime? I don't think it does.
    – t3chb0t
    Jul 18 at 11:03










  • I changed it so it can calculate nth number prime where n is taken as an input
    – Naia Suzuki
    Jul 18 at 11:28






  • 1




    I would focus on increasing readability, maybe you will then spot the answer to your question by yourself.
    – Slampen
    Jul 19 at 7:16






  • 1




    This is Project Euler Problem 7 for those who want the link.
    – rossum
    Jul 29 at 12:19







1




1




You might want to take a look at the answers of my question.
– Denis
Jul 18 at 8:06




You might want to take a look at the answers of my question.
– Denis
Jul 18 at 8:06




2




2




Shouldn't this code calculate the 10.001st prime? I don't think it does.
– t3chb0t
Jul 18 at 11:03




Shouldn't this code calculate the 10.001st prime? I don't think it does.
– t3chb0t
Jul 18 at 11:03












I changed it so it can calculate nth number prime where n is taken as an input
– Naia Suzuki
Jul 18 at 11:28




I changed it so it can calculate nth number prime where n is taken as an input
– Naia Suzuki
Jul 18 at 11:28




1




1




I would focus on increasing readability, maybe you will then spot the answer to your question by yourself.
– Slampen
Jul 19 at 7:16




I would focus on increasing readability, maybe you will then spot the answer to your question by yourself.
– Slampen
Jul 19 at 7:16




1




1




This is Project Euler Problem 7 for those who want the link.
– rossum
Jul 29 at 12:19




This is Project Euler Problem 7 for those who want the link.
– rossum
Jul 29 at 12:19










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










As mentioned in the comments, you should go for more readable code:



1) Split by responsibility:



One method for user input:



ulong GetPrimeIndex() // TODO: Get User input from Console 


The algorithm itself:



ulong FindNthPrime(ulong n) // TODO: the algorithm 


print the result...



public static void Main(string args)

ulong primeIndex = GetPrimeIndex();

Stopwatch watch = Stopwatch.StartNew();
ulong nthPrime = FindNthPrime(primeIndex);
watch.Stop();

Console.WriteLine($"primeIndex prime is: nthPrime");
Console.WriteLine($"Elapsed time: watch.ElapsedMilliseconds");



2) Naming:



In C# local variable names use camelCase: largestprime should be largestPrime




Because primes are always positive, you should use ulong instead of long. It will give you a broader range to operate in.




I think you're missing a couple of breaks in these tests:




 if (primearray[x] * primearray[x] > potentialprime)
break;
if (prime > 3 && (potentialprime % primearray[x] == 0 || potentialprime % primearray[x+1] == 0 || potentialprime % primearray[x+2] == 0))
isprime = false;
if (prime <= 3 && potentialprime % primearray[x] == 0)
isprime = false;



It should be:



 if (primearray[x] * primearray[x] > potentialprime)
break;
if (prime > 3 && (potentialprime % primearray[x] == 0 || potentialprime % primearray[x + 1] == 0 || potentialprime % primearray[x + 2] == 0))

isprime = false;
break;

if (prime <= 3 && potentialprime % primearray[x] == 0)

isprime = false;
break;




Be aware that




primearray[x] * primearray[x]




may overflow the domain range. You should maybe make the check by sqrt instead?






share|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199731%2fproject-euler-find-10-001st-prime%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    As mentioned in the comments, you should go for more readable code:



    1) Split by responsibility:



    One method for user input:



    ulong GetPrimeIndex() // TODO: Get User input from Console 


    The algorithm itself:



    ulong FindNthPrime(ulong n) // TODO: the algorithm 


    print the result...



    public static void Main(string args)

    ulong primeIndex = GetPrimeIndex();

    Stopwatch watch = Stopwatch.StartNew();
    ulong nthPrime = FindNthPrime(primeIndex);
    watch.Stop();

    Console.WriteLine($"primeIndex prime is: nthPrime");
    Console.WriteLine($"Elapsed time: watch.ElapsedMilliseconds");



    2) Naming:



    In C# local variable names use camelCase: largestprime should be largestPrime




    Because primes are always positive, you should use ulong instead of long. It will give you a broader range to operate in.




    I think you're missing a couple of breaks in these tests:




     if (primearray[x] * primearray[x] > potentialprime)
    break;
    if (prime > 3 && (potentialprime % primearray[x] == 0 || potentialprime % primearray[x+1] == 0 || potentialprime % primearray[x+2] == 0))
    isprime = false;
    if (prime <= 3 && potentialprime % primearray[x] == 0)
    isprime = false;



    It should be:



     if (primearray[x] * primearray[x] > potentialprime)
    break;
    if (prime > 3 && (potentialprime % primearray[x] == 0 || potentialprime % primearray[x + 1] == 0 || potentialprime % primearray[x + 2] == 0))

    isprime = false;
    break;

    if (prime <= 3 && potentialprime % primearray[x] == 0)

    isprime = false;
    break;




    Be aware that




    primearray[x] * primearray[x]




    may overflow the domain range. You should maybe make the check by sqrt instead?






    share|improve this answer



























      up vote
      2
      down vote



      accepted










      As mentioned in the comments, you should go for more readable code:



      1) Split by responsibility:



      One method for user input:



      ulong GetPrimeIndex() // TODO: Get User input from Console 


      The algorithm itself:



      ulong FindNthPrime(ulong n) // TODO: the algorithm 


      print the result...



      public static void Main(string args)

      ulong primeIndex = GetPrimeIndex();

      Stopwatch watch = Stopwatch.StartNew();
      ulong nthPrime = FindNthPrime(primeIndex);
      watch.Stop();

      Console.WriteLine($"primeIndex prime is: nthPrime");
      Console.WriteLine($"Elapsed time: watch.ElapsedMilliseconds");



      2) Naming:



      In C# local variable names use camelCase: largestprime should be largestPrime




      Because primes are always positive, you should use ulong instead of long. It will give you a broader range to operate in.




      I think you're missing a couple of breaks in these tests:




       if (primearray[x] * primearray[x] > potentialprime)
      break;
      if (prime > 3 && (potentialprime % primearray[x] == 0 || potentialprime % primearray[x+1] == 0 || potentialprime % primearray[x+2] == 0))
      isprime = false;
      if (prime <= 3 && potentialprime % primearray[x] == 0)
      isprime = false;



      It should be:



       if (primearray[x] * primearray[x] > potentialprime)
      break;
      if (prime > 3 && (potentialprime % primearray[x] == 0 || potentialprime % primearray[x + 1] == 0 || potentialprime % primearray[x + 2] == 0))

      isprime = false;
      break;

      if (prime <= 3 && potentialprime % primearray[x] == 0)

      isprime = false;
      break;




      Be aware that




      primearray[x] * primearray[x]




      may overflow the domain range. You should maybe make the check by sqrt instead?






      share|improve this answer

























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        As mentioned in the comments, you should go for more readable code:



        1) Split by responsibility:



        One method for user input:



        ulong GetPrimeIndex() // TODO: Get User input from Console 


        The algorithm itself:



        ulong FindNthPrime(ulong n) // TODO: the algorithm 


        print the result...



        public static void Main(string args)

        ulong primeIndex = GetPrimeIndex();

        Stopwatch watch = Stopwatch.StartNew();
        ulong nthPrime = FindNthPrime(primeIndex);
        watch.Stop();

        Console.WriteLine($"primeIndex prime is: nthPrime");
        Console.WriteLine($"Elapsed time: watch.ElapsedMilliseconds");



        2) Naming:



        In C# local variable names use camelCase: largestprime should be largestPrime




        Because primes are always positive, you should use ulong instead of long. It will give you a broader range to operate in.




        I think you're missing a couple of breaks in these tests:




         if (primearray[x] * primearray[x] > potentialprime)
        break;
        if (prime > 3 && (potentialprime % primearray[x] == 0 || potentialprime % primearray[x+1] == 0 || potentialprime % primearray[x+2] == 0))
        isprime = false;
        if (prime <= 3 && potentialprime % primearray[x] == 0)
        isprime = false;



        It should be:



         if (primearray[x] * primearray[x] > potentialprime)
        break;
        if (prime > 3 && (potentialprime % primearray[x] == 0 || potentialprime % primearray[x + 1] == 0 || potentialprime % primearray[x + 2] == 0))

        isprime = false;
        break;

        if (prime <= 3 && potentialprime % primearray[x] == 0)

        isprime = false;
        break;




        Be aware that




        primearray[x] * primearray[x]




        may overflow the domain range. You should maybe make the check by sqrt instead?






        share|improve this answer















        As mentioned in the comments, you should go for more readable code:



        1) Split by responsibility:



        One method for user input:



        ulong GetPrimeIndex() // TODO: Get User input from Console 


        The algorithm itself:



        ulong FindNthPrime(ulong n) // TODO: the algorithm 


        print the result...



        public static void Main(string args)

        ulong primeIndex = GetPrimeIndex();

        Stopwatch watch = Stopwatch.StartNew();
        ulong nthPrime = FindNthPrime(primeIndex);
        watch.Stop();

        Console.WriteLine($"primeIndex prime is: nthPrime");
        Console.WriteLine($"Elapsed time: watch.ElapsedMilliseconds");



        2) Naming:



        In C# local variable names use camelCase: largestprime should be largestPrime




        Because primes are always positive, you should use ulong instead of long. It will give you a broader range to operate in.




        I think you're missing a couple of breaks in these tests:




         if (primearray[x] * primearray[x] > potentialprime)
        break;
        if (prime > 3 && (potentialprime % primearray[x] == 0 || potentialprime % primearray[x+1] == 0 || potentialprime % primearray[x+2] == 0))
        isprime = false;
        if (prime <= 3 && potentialprime % primearray[x] == 0)
        isprime = false;



        It should be:



         if (primearray[x] * primearray[x] > potentialprime)
        break;
        if (prime > 3 && (potentialprime % primearray[x] == 0 || potentialprime % primearray[x + 1] == 0 || potentialprime % primearray[x + 2] == 0))

        isprime = false;
        break;

        if (prime <= 3 && potentialprime % primearray[x] == 0)

        isprime = false;
        break;




        Be aware that




        primearray[x] * primearray[x]




        may overflow the domain range. You should maybe make the check by sqrt instead?







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jul 19 at 10:24


























        answered Jul 19 at 10:12









        Henrik Hansen

        3,7781417




        3,7781417






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199731%2fproject-euler-find-10-001st-prime%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Python Lists

            Aion

            JavaScript Array Iteration Methods