Results 1 to 5 of 5

Thread: Decent way to find prime numbers (simple and fast)

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Decent way to find prime numbers (simple and fast)

    I had heard a long time ago that this was the best way to find primes:
    1) Fill an array with all every prime you have found so far (usually start with 2 and 3 as 2 elements that are already in the array)
    2) If you want to know if X is prime then divide it by every number in the array.
    3) If it X mod Array(i) = 0 then it goes in evenly and it can't be prime.
    4) If X is prime then add it to the array and continue

    I tried this method and found it was very slow. Here is what I did that gave the same result in 1/1000th of the time:
    If you want to know if X is prime then MOD it with every number from I to Sqrt(I) and if none of those = 0 then it is prime.

    This may seem obvious to some people, but I thought the first method would be quicker for some reason.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Decent way to find prime numbers (simple and fast)

    Remember the contest? What did Merri do?


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Decent way to find prime numbers (simple and fast)

    Strike that, the contest was before you joined I think

    The first contest we had here was to generate the first 1000 prime numbers. Merri won from what I remember. The code submissions are in the subforum in the Contest Area


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: Decent way to find prime numbers (simple and fast)

    yeah i wrote a program myself, It was slow, but those contest submissions are incredible. I was impressed.
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

  5. #5

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Decent way to find prime numbers (simple and fast)

    I knew about the contest. I posted what I did in here because I didn't want anyone else to be under the false impression that I was under (about checking all previous prime numbers). I would have used Merri's code, but I wanted to figure out the problem on my own (and I needed the first 1 million prime numbers).
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width