Talk:Some Basic and Inefficient Prime Number Generating Algorithms

well, for reference, I ran your App B code on my laptop under Python 2.6 Timing was 666 sec to generate the prime set up to: 4.29 E7 with 2598870 members, in line with the article. System basics are AMD Turion64 X2, 2Gb ram --Billymac00 (talk) 03:54, 17 September 2009 (UTC)

Pari/GP
Now, I wrote the simplest of scripts for Pari/GP around its isprime. The time to the same prime count was 300 sec. The only nuance was avoiding multiples of 5. Same laptop as previous.--Billymac00 (talk) 19:51, 20 September 2009 (UTC)

reply to Billymac00
Thank you for the suggestion about Pari/GP, I will try to implement a version of PG7.8 using that language and update with the new data, if I am successful...It may take some time, since I am currently unfamiliar with and do not yet have the Pari/GP add-on in my version of Python. If you have already done this, please let me know. I am right now downloading SAGE, which includes Pari/GP. Thanks again. --68.186.212.217 (talk) 18:33, 22 September 2009 (UTC)

script
I posted my script at my weblink: it is the time1234, note it uses isprime, not ispseudoprime. --Billymac00 (talk) 01:27, 25 September 2009 (UTC)

rename
I think this text would fit well in the book Algorithm Implementation. So I suggest renaming this "book" from A More Efficient Prime Number Generating Algorithm to Algorithm Implementation/A More Efficient Prime Number Generating Algorithm, making it a section of that larger book. --DavidCary (talk) 23:51, 26 October 2009 (UTC)


 * I've renamed this to Efficient Prime Number Generating Algorithms. I don't think it belongs in Algorithm Implementations as the stated purpose of that book is to "to show how common algorithms are written in various programming languages" [emphasis mine]. This book seems to have the scope to detail techniques to create efficient prime number generating algorithms. Comparisons between languages is a different topic. --Swift (talk) 14:22, 16 February 2010 (UTC)

Considered Sieve of Eratosthenes?
I'm confused by this book. Why are you factoring primes when sieving is "known" to be one of the fastest ways to generate primes.

If you look at Chris Caldwell's prime pages: http://primes.utm.edu or George Woltman's http://mersenne.org, they both highly recommend sieving in their algorithm sections for finding small primes (anything less than 100 digits).

Why factor when there is an algorithm that has been around for ~2200 years? If you were trying to test primality, your methods look like a good start, but for generating primes, sieving can't be beat. The Sieve of Atkin is even faster (according to Wikipedia). At the very least Sieving should be mentioned in the book and dismissed if there is a valid reason.

All of the below functions are >20x faster than v7.8 provided in the text.

Here is the fastest Sieve of Eratosthenes for python that I found using Google: http://www.daniweb.com/code/snippet217082.html

The only problem is that this Sieve is very memory inefficient. So my first iteration of improvement removes the even numbers from the start (which speeds it up a little bit):

Then I decide to go with a custom bitset which improves memory even more but slows it down a bit (still very fast):

Then I realized that we are creating a generator here. Why not use python generators for this? Also, since the basic sieve from daniweb.com is so memory inefficient, we should break down the overall length into manageable chunks and sieve each section separately. The daniweb.com version breaks on my machine once we get above ~20,000,000. In the example below iterSize is defined as 10,000,000, but it can be dropped to 1,000,000 for a memory reduction (at a little speed cost). This generator will generate all primes up to 1e14 (or iterSize**2) given enough time, and is nearly as efficient as the daniweb.com one, while not breaking memory usage limits.

Just my thoughts

Zerothbase (talk) 04:12, 7 August 2010 (UTC)

Simple drastic improvement overlooked
In all your code variants it is enough for "test" to be set only ONCE.

It changes the empirical complexity from n^1.5 to n^1.37. This means drastic speedups, especially for bigger numbers:

WillNess (discuss • contribs) 00:40, 12 July 2011 (UTC)

You are quite right. It does make sense to end the prime verification process as soon as the potential prime has been eliminated as a prime candidate. I did overlook this in the simple versions, but the "break" line was included in the most advanced version(PG7.8). Thank you for noticing.
 * BTW I ran your code to check out its speed (had to make few changes to it so it'll run). As expected, sieve of Eratosthenes is much faster. :) WillNess (discuss • contribs) 07:12, 8 August 2011 (UTC)

A Much Simpler Method to locate Prime Numbers
The inherent deficiency with most "prime number" generators is the literally thousands of calculations that are made and discarded. I have developed an algorithm in which every calculation is used. I began my study along the same basic lines as most do: the divide and conquer method. Then, progressed to dividing the prime number candidate by all previously determined prime numbers until either an even quotient resulted - thusly a composite or the square of the prime factor exceeded the candidate - thusly a prime. I realized the immediate deficiency seeing that I was performing an excess amount of calculations to determine primality.

As I was exploring more efficient sieving methods, another concern arose: how to store prime numbers in an efficient compact manner. Now, fifteen years into this study, I have succeeded in developing an efficient and compact storage method and developed an relatively small algorithm that locates prime numbers with a method that every calculation cycle is saved for future inclusion in the data string.

I have presented certain aspects of my work on Mr. Caldwell's page being met with the expected backhandedness a prevalent reaction of the select self proclaimed gurus in this particular field when an unknown upstart challenges the status quo. My qualifications in the field of mathematics is two years of high school algebra, a miserable showing in high school geometry and a worse showing in high school trigonometry (1970) and never attempting calculus. My computer programming skills are very "old school - late '70's". I've programmed in CoBOL, Fortran and several versions of basic and visual basic. Lately, I have learned limited HTML, java scripting and asp scripting. To build this algorithm, I used a now ancient laptop using VB6. And, at this point in my study, I can not move forward until I can gain access to at least a mini-main frame that will allow me to run this algorithm wide open. This algorithm is currently capable of locating prime numbers up to 32767 places long, albeit in a base other than base 10 and easily expandable to locate prime numbers as long as an estimated 1.25 million places base10. (I am considering developing a "wikibook" detailing in depth my study to date, although I have a keen reservation in doing so considering the potential impact this algorithm may have on cryptology.) -- the above text was added by Carl Bohn (discuss | contribs) on 03:18, 9 December 2013 -- WillNess (discuss • contribs) 12:50, 15 February 2014 (UTC).

New Algorithm
While the algorithm cannot compete in speed, it is interesting in its simplicity. It was developed by a fella named Pedro Caceres, and I only just learned of it. Essentially, besides 2&3 all primes are adjacent to multiples of 6, and Caceres' algorithm filters out the composites adjacent to same, using no division. Caceres has it written in Python, I have it is Pari/GP. Mr. Caceres published email is pcaceres@comcast.net.--Billymac00 (discuss • contribs) 13:39, 23 November 2017 (UTC)

OEIS A271116
Out of interest, the equation of this sequence OEIS A271116 produces every prime number but with a limited amount of composites as well. Perhaps it is useful as a pre-filter. A pending comment made to the sequence claims that the first million terms include only only 908 nonprimes. --Billymac00 (discuss • contribs) 01:21, 4 October 2020 (UTC)