from math import sqrt

def is_prime(n):

for i in range(2, sqrt(n)):

if n % i == 0:

return False

return True

import math

import numpy

def prime6(upto):

primes=numpy.arange(3,upto+1,2)

isprime=numpy.ones((upto-1)/2,dtype=bool)

for factor in primes[:int(math.sqrt(upto))]:

if isprime[(factor-2)/2]: isprime[(factor*3-2)/2:(upto-1)/2:factor]=0

return numpy.insert(primes[isprime],0,2)

if n==2:

print "prime"

if n==1:

print "neither prime nor composite"

if n>2:

for i in range(2,n):

if n%i==0:

print "composite"

break

i=i+1

else:

print "prime"

else:

print "enter correct input" ]]>

c=0

for i in range(number):

if number%(i+1)==0:

c=c+1

if c>2:

print number,"is not prime number"

else:

print number,"is prime number"

num=raw_input("enter a number:")

num=int(num)

isPrimenumber(num) ]]>

try to read about miller-tabin algorythm

P.S. sorry for my english ðŸ˜‰

]]>Dr. Blake's approach is via a randomization channel. Code is also presented. I tried to cut and paste but was unsuccessful.

]]>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

1 | import math |

and replace the line

1 | while i <= math.sqrt(n): |

by the equivalent sentence

1 | while i*i <= n: |

that uses only integer arithmetics.

]]>Again, thanks for the info.

]]>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

]]>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.

]]>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.

]]>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. ]]>

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.

]]>http://www.math.princeton.edu/~annals/issues/2004/Sept2004/Agrawal.pdf

]]>1 2 3 4 5 6 7 8 | bool isPrime(long p) { long maxfactor = static_cast(sqrt(p) + 1); // round up for (long n = 2; n < maxfactor; ++n) if (p % n == 0) return false; return true; } |

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.)

]]>`sub is_prime {`

my ($n) = @_;

$n = abs($n);

my i = 2;

while ($i

@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.

]]>1 2 3 4 5 6 7 8 9 10 11 12 13 | class Integer def is_prime? i=3 return false if self % 2 == 0 and self != 2 ss = sqrt(self).floor return false if ss**2 == self while i <= ss return false if self % i == 0 i += 2 end return true end end |

1 2 3 4 5 6 7 8 9 10 11 | class Integer def is_prime? i=3 return false if self % 2 == 0 and self != 2 while i < self/2 return false if self % i == 0 i += 2 end return true end end |

Other optimizations include not testing every single number as divisor, but only new prime numbers. Check the Sieve of Eratosthenes for details.

--rbu

]]>