The following program returns True if a number is prime or False otherwise. I am proud of this code, as it implements dead code. Upon the first positive condition in the if statement nested in the while loop, the program terminates, ignoring any further code following. As far as I can tell, this is the most efficient way (not necessarily the fastest) to test if a number is prime or not. This means that this function has an order of O(n-2) (it doesn’t test against 1 and itself) as a worst case scenario, and O(n/m) where m is the first factor of n as a best case. As elementary as it is, I think it’s pretty slick. All handled in 9 very readable lines of code.
UPDATE: Thanks to some observant readers, I have come to the realization that I only need to loop to the square root of n, and not n, as the square root of n is the largest factor. This brings the order to O(sqrt(n)-2) at the worst case scenario, and O(1) as the best case.
def is_prime(n): import math n = abs(n) i = 2 while i <= math.sqrt(n): if n % i == 0: return False i += 1 return True

{ 21 } Comments
One simple optimization: The while loop can stop when i > sqrt(n). If no factors were found until then, there won’t be any in (sqrt(n),n].
Other optimizations include not testing every single number as divisor, but only new prime numbers. Check the Sieve of Eratosthenes for details.
–rbu
[Reply]
There are A LOT faster algorithms doing that job. They make use of number theory and work in polynomial time (in terms of number length)
[Reply]
Good stuff! I rubified it and started playing with it, and sped it up thusly (don’t forget to change the < back to a <, your software hates it):
[Reply]
Also rbu is right, doing while i <= sqrt(n) (or sqrt(self) in my ruby) it’s very much faster.
[Reply]
what are you talking about? this is very trivial code that is not even remotely efficient at what it’s supposed to do.
[Reply]
Seriously, WTF? This is the opposite of slick.
[Reply]
Last post I promise! Sure there are plenty of faster algorithms. But this is reasonably fast, reasonably efficient and accomplished with reasonably little code. It’s also very useful for both testing primeness and enumerating primes. Anyway I made it faster still and more accurate (1 is not a prime number). This enumerated all the primes from 1 to 1M in 34 seconds for me:
[Reply]
@rbu- Ahh, good call. Makes perfect sense.
@Swistak- As I mentioned in the post, there are probably faster algorithms.
@Rocco Stanzione- No problem. Keep posting.
Thanks for the code, by the way.
@Pansen & randomwalker- Thanks for the informative comments. Very well thought through.
[Reply]
Perl version anyone?
sub is_prime {my ($n) = @_;
$n = abs($n);
my i = 2;
while ($i
[Reply]
Your post got me thinking about python and optimizations. So I googled about and found a few algorithms that are pretty fast at determining primeness. I posted them here: http://www.tiawichiresearch.com/?m=200709
[Reply]
C++ version, anyone?
Just for reference, to test each number in [1, 10^6], the C/C++ version is a bit more than an order of magnitude faster than the sqrt-optimized version of the above python; python (2.10 sec versus 39.5 sec).
Programming posts are always nice to see in my reader; keep up the learning!
(Side note: I can’t put a less-than sign in my comment. It appears that the post page thinks its a tag start, so I tried < but that shows literally, as seen above.)
[Reply]
You may find this interesting:
http://www.math.princeton.edu/~annals/issues/2004/Sept2004/Agrawal.pdf
[Reply]
In 2004, Agrawal, Kayal, and Saxena published this paper:
http://www.math.princeton.edu/~annals/issues/2004/Sept2004/Agrawal.pdf
in which they present a polynomial time algorithm to determine if a number is prime or composite.
Note that it is a polynomial time algorithm in the size of the input.
I hope you find that article interesting. In addition to proving a deep result, the references section will point you to just about everything you could care to know about primes.
[Reply]
Hehe, as usual I got carried away and programmed multiple versions myself in Python (including a sieve algorithm and a caching version of your algorithm).
For the critics here: the simple algorithm (with the sqrt) is pretty fast at least up until (2^31)-1.
If you want to be fast for big primes, you would probably pick a different language anyway.
[Reply]
Careful of sweeping statements like “As far as I can tell, this is the most efficient way (not necessarily the fastest) to test if a number is prime or not.”
When its hardly efficient, or elegant, and efficiency in software implies speed.
If I were to ask you whether or not 578438089702734102192371840427834912121157 were prime or not how long would it take to identify that? PGP has been implementing prime testing and deriving primes in far more efficient ways for a long time, and can determine whether or not a 4096bit number is prime in a few seconds.
Exploit sieves and “don’t care” cases faster.
[Reply]
I don’t think you are using the O notation correctly. Using binary search to search through a list of n ordered numbers takes O(sqrt(n)). Here, n is not the number of inputs, it’s the input itself.
Now, if you were saying n was the size of the input number, I think you’ll find that your notation is still flawed. This is not a poly time algorithm. If it were, you would be able to break several encryption algorithms that rely on the hardness of the primality problem, as well (I believe) P=NP.
It’s been a while since I’ve done algorithmics in school, but I think you’ll find I am right.
C.
[Reply]
A couple of comments regarding the complexity of this algorithm.
First, you don’t include lower-order factors in big-O notation. An algorithm that takes n-1 steps is still O(n). An algorithm that takes n^2+5n+2 steps is O(n^2), or simply Polynomial Time when speaking in terms of complexity classes.
Second, you’re ignoring the fact that, for large numbers, modulo is not a constant time operation. The O(sqrt(n)) complexity is only correct if n is the size of your number and it fits in the hardware. This isn’t true in general, so it’s not very useful for the sorts of reasoning big-O notation was invented for.
To be general, you need to define the algorithm in terms of the number of bits in your input number. For an integer N, it takes log N + 1 bits to store it. That means that the complexity in terms of the input size is O(2^(n/2)). This is exponential, i.e. very bad. A 1024 bit integer gives you a running time of essentially 2^512, which is definitely intractable.
As other people have stated, there are much better algorithms for primality testing, and the one you’re using is worthless for crypto applications.
My reference for this comment: Complexity and Cryptography: An Introduction
[Reply]
@Levi- Fair enough. My inexperience really shows through on things like this. I need to spend more time on math-related algorithms analyzing their complexity and what’s going on under the hood I think.
Again, thanks for the info.
[Reply]
Hi Aaron,
First of all congratulations for your interesting blog.
Let me suggest an improvement to make your code much more efficient.
I do not know Python, but using my knowledge on other programming languages I think that I can understand your code pretty well.
You can use only integer arithmetic doing two minor changes.
Eliminate the sentence
and replace the line
by the equivalent sentence
while i*i <= n:that uses only integer arithmetics.
[Reply]
Review this publication: Prime Sequence Homology by Daniel Blake et al; Fourrier University, Grenoble
Dr. Blake’s approach is via a randomization channel. Code is also presented. I tried to cut and paste but was unsuccessful.
[Reply]
your story is sux, try to test, for example, 99999999977 or 4740914805200312594461
try to read about miller-tabin algorythm
P.S. sorry for my english
[Reply]
Post a Comment