Fast prime finder??
|
07-28-2016, 08:22 PM
Post: #1
|
|||
|
|||
Fast prime finder??
Hi All,
I am looking for the fastest algorithm to: 1) Determine if a number is a prime. 2) Find the next prime after a given number. I plan to include the best answer in my HHC2016 presentation. The keyword here is fast computational-wise. Thanks! Namir |
|||
07-29-2016, 02:50 AM
Post: #2
|
|||
|
|||
RE: Fast prime finder??
(07-28-2016 08:22 PM)Namir Wrote: Hi All, This problem has been studied a lot, and the algorithms are well known. I'm not sure what you are aiming at: calculator code? any code? or just the algorithm? what magnitude of numbers do you need to test? I wrote a few routines for newRPL that use a bitmap table for primes <120000. This allows you to both test for primality and get the next prime number (given a number <120000) extremely fast, which speeds up the brute force primality test for numbers up to 120000^2 = 1.44e10. For larger numbers it falls back to brute force (test the remainder after division by every prime <= sqrt(N)). In the future it will use Miller-Rabin or similar pseudo-prime algorithm for large numbers, but that's not implemented yet. I recently tested the number 9,999,999,967 per this thread: http://www.hpmuseum.org/forum/thread-6567.html Since it's < 1.44e10 I got a very fast result at 0.057s. While the tables are encoded in a creative way, there's nothing to be proud of, as it's basically a brute force primality test accelerated with look-up tables. |
|||
07-29-2016, 07:27 AM
Post: #3
|
|||
|
|||
RE: Fast prime finder??
I am looking for the algorithm(s).
Namir |
|||
07-29-2016, 12:03 PM
Post: #4
|
|||
|
|||
RE: Fast prime finder??
I found what I was looking for on the Internet.
:-) Namir |
|||
08-01-2016, 05:24 PM
Post: #5
|
|||
|
|||
RE: Fast prime finder??
(08-01-2016 01:45 PM)Mike (Stgt) Wrote:(07-29-2016 12:03 PM)Namir Wrote: I found what I was looking for on the Internet. I was hoping to find a clever equation that calculates primes. I did find some code that uses a loop (iterating over odd numbers) that looks for primes. Namir |
|||
08-01-2016, 07:00 PM
Post: #6
|
|||
|
|||
RE: Fast prime finder??
(08-01-2016 05:24 PM)Namir Wrote: I was hoping to find a clever equation that calculates primes. I did find some code that uses a loop (iterating over odd numbers) that looks for primes. I think Mike was suggesting that readers of this thread likely would also be interested in such a solution, and it would be courteous to share the link you found. Unless that steals thunder for your HHC presentation, which I am looking forward to as always. --Bob Prosperi |
|||
08-01-2016, 09:59 PM
Post: #7
|
|||
|
|||
RE: Fast prime finder??
There are really no formulas for primes. The primes seem to occur at random (with a known density of ln(N)/N for the number of primes up to N). One can do clever sieving and use some pseudo-prime tests (like Fermat's) to check.
For searching (not on calculators), I've often used a storage of 30 numbers in 8 bits. Checking all numbers not divisible by 30, one get that primes have remainder of (1,7,11,13,17,18,23,29). I pack 30 numbers into one byte and set the appropriate bit if that number is prime. One can go higher with more complicated packing. This is still linear. One can do a bit better. A more efficient packing is to store the difference between primes as primes get large. This is a bit complicated. None of this tells whether a number is prime, but most prime checking methods work better with a bunch of stored primes. https://en.wikipedia.org/wiki/Primality_test gives some methods. |
|||
08-02-2016, 12:01 AM
Post: #8
|
|||
|
|||
RE: Fast prime finder??
(08-01-2016 09:59 PM)ttw Wrote: There are really no formulas for primes. The primes seem to occur at random (with a known density of ln(N)/N for the number of primes up to N). One can do clever sieving and use some pseudo-prime tests (like Fermat's) to check. Thanks ttw for the link. That Wikipedia page has p-code that seems to be a very good prime tester, compared to the few other similar functions I have found so far. Namir |
|||
08-02-2016, 08:28 PM
(This post was last modified: 08-03-2016 11:02 AM by Namir.)
Post: #9
|
|||
|
|||
RE: Fast prime finder??
(08-02-2016 03:34 PM)Mike (Stgt) Wrote: I have here a paper titeled "Die Methode der häufigen Zeugen", with the subtitle "Der randomisierte Primzahltest von Solovay und Strassen". I got it from a very nice lad - for me only, not for distribution. Bing for 'The Solovay-Strassen Algorithm' and you'll find some hits about it. Thanks for the tip. I am looking for the topic AND source code implementation to see how coders deal with raising numbers to powers of even larger numbers!! :-) Namir |
|||
08-03-2016, 06:30 AM
Post: #10
|
|||
|
|||
RE: Fast prime finder??
(08-02-2016 08:28 PM)Namir Wrote: I am looking for the topic AND source code implementation to see how coders deal with raising numbers to powers of even larger numbers!!It's not just exponentiation, which means lots of digits, it's modular exponentiation, which means you don't need to keep all the digits. Rosetta code can be a good resource: https://rosettacode.org/wiki/Modular_exponentiation |
|||
08-03-2016, 08:36 AM
Post: #11
|
|||
|
|||
RE: Fast prime finder??
For exponentiation of large numbers, a reasonable (if not optimal) strategy is to use binary exponentiation followed by a modular reduction at each step. Intermediates can be held to no more that twice the number of digits as the numbers of interest. Optimal orders of multiplication (factorization of the exponent or a tree structure, see Knuth vol. 2) are only useful if the same exponent is used repeatedly.
|
|||
08-03-2016, 11:07 AM
(This post was last modified: 08-03-2016 11:07 AM by Namir.)
Post: #12
|
|||
|
|||
RE: Fast prime finder??
I found on the Internet Java, C++, and Python listings for the Solovay algorithm. I translated the C++ code into Excel VBA code. The code worked fine except it would let a few composite numbers slip as primes. I added a loop to filter for the first few primes (from 2 to 19) and that make the code much better. I will post the VBA code later today.
Namir |
|||
08-03-2016, 11:50 AM
(This post was last modified: 08-03-2016 08:48 PM by Namir.)
Post: #13
|
|||
|
|||
RE: Fast prime finder??
OK here is the VBA code that tests a set of prime finder routines. I frequently use Excel VBA first to write new code. The spreadsheet is very useful to view intermediate and final results. From there I convert code to Matlab, HP Prime, True Basic, and so on.
The spreadsheet needs the following input: 1) First number in the range of primes in cell B1. 2) Last number in the range of primes in cell B2. 3) Number of iterations used in the Solovoy algorithm in Cell B3. Cells A1, A2, and A3 contain the text "First", "Last", and "Iters". To run the test invoke the macro (i.e. SUB) Go which tests five different methods. I included the number of iterations used to determine the primes (which is a fixed number for the Solovoy algorithm): Code:
In the case of the Solovoy function, I added the following VBA code to the translated C++ code to filter out composite numbers that were coming out as primes: Code:
The prime finder results imrpove from NextPrime to NextPrim1 ... to NextPrime4 and NextPrime5. I included VBA Excel code since Excel is very popular, allowing folks who read this message to easily copy and paste the VBA code, setup the spreadsheet, and run the macro Go. Namir |
|||
08-04-2016, 11:54 PM
Post: #14
|
|||
|
|||
RE: Fast prime finder??
I have working code for the HP-Prime which implements the Solovay algorithm. I will make a brief mention of this algorithm in one of my HHC2016 talk.s
Namir |
|||
08-05-2016, 12:46 AM
(This post was last modified: 08-05-2016 12:50 AM by Claudio L..)
Post: #15
|
|||
|
|||
RE: Fast prime finder??
(08-04-2016 11:54 PM)Namir Wrote: I have working code for the HP-Prime which implements the Solovay algorithm. I will make a brief mention of this algorithm in one of my HHC2016 talk.s According to Wikipedia, Solovay-Strassen was superseded by Miller-Rabin and Baillie-PSW methods. Miller-Rabin is one of the most widely used and studied. https://en.wikipedia.org/wiki/Miller–Rab...ality_test Depending on the number of primes and the values of the primes you test against, it works better or worse. Some people spent a lot of time looking for the best values to maximize its performance: https://miller-rabin.appspot.com The best cases are quite amazing: with only one test it can find all primes up to 341531 without letting anything slip, while only 7 tests guarantee perfect results up to 64-bit integers. Baillie-PSW seems like a cocktail of 4 other filters, one after another to get even better results, at the expense of slightly higher complexity. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)