Re: returning none when it should be returning a list?



Edward Elliott wrote:

randomtalk@xxxxxxxxx wrote:
The function basically takes in a list of all the prime number
found, it takes the next number to be tested for (next) and the
limit it will go up to. It divide next by the list of previous
prime numbers if next is not bigger than lim, count up all the
times where it's not evenly divdided, if the result is equal the
length of prime number, then we have another prime number, lather
rinse and repeat :)

Your basic algorithm as described above sounds workable, but can be
made more efficient (I didn't look closely at your code for bugs). I
won't even go near the complicated number theory algorithms.

1. integer mod will be faster than floating point mod (unless you're
getting into numbers too large to store efficiently as ints, but
you'll probably run into other limitations before you get near that
point in this code).

2. you can stop as soon as you find a zero remainder, no need to keep
testing the rest of the primes in the list.

3. you can stop testing numbers at the halfway point. there are no
primes smaller than 2, so no number x can have a factor larger than
x/2.

4. in fact you can do even better. a simple proof by contradiction
shows that if primes 1..y don't divide x and y^2 > x then x must be
prime. put another way, once you test up to the square root of x,
there's no point continuing. if one factor were bigger than sqrt(x),
the other would have to be smaller than sqrt(x). but you've already
tested the factors smaller than sqrt(x), so there can't be any
factors.

The Prime Number article[1] on Wikipedia has a nice little Python
snippet implementing this algorithm (more or less). See the Finding
Prime Numbers section.

5. you can do better than checking every odd number (next+2) to find
the next prime, but I'm too tired to look into it right now. it may
require more complex machinery.

Locating primes is an interesting challenge because of the seemingly
endless grades of refinements over simple brute-force search. Like I
said though, if speed and size aren't concerns, your algorithm is
fine.

Further reading: the Sieve of Eratosthenes[2] is a relatively simple
and fun little algorithm to implement (also has a size issue in that it
requires a list of numbers from 2 up to the largest number you wish to
test for primality, and I don't think it's any faster than the
algorithm above). A modern refinement called the Sieve of Atkin[3] is
also interesting, a bit faster, although rather more complicated.

If you want to look further into the subject, see the Primality Test
article[4] on Wikipedia (mentions things like probabilistic testing,
the Miller-Rabin primality test, and such like).

[1] http://en.wikipedia.org/wiki/Prime_number
[2] http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
[3] http://en.wikipedia.org/wiki/Sieve_of_Atkin
[4] http://en.wikipedia.org/wiki/Primality_test


Dave.
--

.



Relevant Pages

  • Re: Evaluation of MegaSnakeOil by "expert" + "Primes in P"
    ... MAJOR WORLDWIDE MATHEMATICAL BREAKTHROUGH – MEGANET CORPORATION ... DETERMINISTIC ALGORITHM IN THE WORLD TO WORK IN POLYNOMIAL TIME ... 1,000 bits primality test - 0.5 Sec ...
    (sci.crypt)
  • Re: Another twin primes conjecture
    ... > being a quick primality test. ... the best known algorithm for this problem works in O). ... Assuming that my understanding of HPC is correct then your HPC-HPC ... Since there are already prime checker implementations no ...
    (sci.math)
  • Re: APL and (very) large arrays
    ... Sort is at best O) depending on the sorting algorithm used, ... As to the OP's question about primes, there are advanced efforts to compute pifor very large values of n, but the only elementary method is to find all primes <= n and count them. ... By representing only the odd values the sieve would require 62.5MB. ...
    (comp.lang.apl)
  • Re: Prime Number Question
    ... were discovered a simply algorithm for generating all prime numbers ... directly, in batches, with each batch being used ... the current best factoring method, the number field sieve, only becomes ... However, given efficient prime factorisation, yes, quite a few crypto ...
    (comp.unix.bsd.openbsd.misc)
  • Re: no lcm in standard library?
    ... which are comparable to the AMD K6 architecture, branch misprediction ... mispredict penalties tilt the algorithm choice in favor of Euclid's ... This implements the binary GCD algorithm which removes the branch ... that it approximates the divide as a 1-bit accurate divide, ...
    (comp.lang.c)