![]() |
Python
1.0
|
Primality testing. More...
Functions | |
def | isPrime (n) |
Tests whether an integer is prime. More... | |
def | isPrime2 (n) |
Using list comprehensions creates a list with all divisors of 'n'. More... | |
def | main (argv=None) |
Variables | |
xrange = range | |
Primality testing.
A prime number (or a prime) is a natural number which has exactly two distinct natural number divisors: 1 and itself.
Only divisors up to \(\lfloor \sqrt{n} \rfloor\) (where \(\lfloor x \rfloor\)is the floor function) need to be tested. This is true since if all integers less than this had been tried, then \(\frac{n}{(\lfloor\sqrt{n}\rfloor+1)} < \sqrt{n}.\) In other words, all possible factors have had their cofactors already tested. Given a factor a of a number n=ab, the cofactor of a is b=n/a.
Trial division, used here, is hopeless for finding any but small prime numbers. Mathematica versions 2.2 and later have implemented the multiple Rabin-Miller test combined with a Lucas pseudoprime test.
Assuming that \(2^{61}-1\) is prime (2305843009213693951), the algorithm will do about \(2^{60}\) divisions (if not using the \(\lfloor\sqrt{n}\rfloor\) limit). Supposing that the computer can perform \(10^{9}\) divisions per sec (1 gigaflop), then this will take approximately 36 years: \(\frac{1152921504606846976}{(1000000000*31536000)}=36.558901085\)
There are several prizes for those who discover large prime numbers, such as a $250,000 to the first individual or group who discovers a prime number with at least 1,000,000,000 decimal digits.
def _04a_prime.isPrime | ( | n | ) |
Tests whether an integer is prime.
n | given integer. |
References _04b_intsqrt.intsqrt(), and xrange.
Referenced by main().
def _04a_prime.isPrime2 | ( | n | ) |
Using list comprehensions creates a list with all divisors of 'n'.
If the list is empty, then 'n' is prime (very inefficient). Ex. n = 100 -> return not [2, 4, 5, 10]
def _04a_prime.main | ( | argv = None | ) |
References _01d_dec2bin.input, and isPrime().
_04a_prime.xrange = range |
Referenced by isPrime().