CompSci 165 Laboratory #2
PROBABILISTICALLY TESTING for PRIMALITY

due May 6 (F week 6)

Requirements

Write a system of programs that will determine whether a large number is prime. The input will be an integer containing up to 1000 decimal digits. The output will be the determination of whether that number is prime (within a certain confidence level) and, if not a prime, what is the next largest prime number.

In order to do this, you will need to implement a number of utility subroutines. Among others, you will need calculation of

x (mod y) modulo
(x + y) (mod N) addition modulo
(xy) (mod N) multiplication modulo
xy (mod N) exponentiation modulo
GCD(x,N) Greatest Common Divisor
J(x,y) the Jacobi function
where all variables may have from 1 to 1000 decimal digits.

Efficient high-level algorithms for some of these subroutines are available to class students.

You should use the Karatsuba method for integer multiplication which takes time O(k1.59) to multiply two k-bit numbers.  However, for multipliers of sufficiently small size you should use "normal" multiplication.  The determination of the best switchover point is one of the items on your list of things to do.

The GNU C++ library has a class called Integer which can handle integers of (almost) arbitrarily large size.  You are not allowed to use this class for this project, as it would make the lab trivial.  Sorry.

Based on your empirical observations (and extrapolations), using 20 iterations in the main loop, how much time would your system require to determine whether

You should be able to check a 100 decimal digit number in under 10 seconds. (I wrote a system that takes about 0.1 seconds for 100-digits on a 2.8 GHz PC.)

User Interface requirements

The user interface for this lab is very simple.  Just repeatedly ask the user for a number and report if it is prime or not.  If it is not, find and print out the next larger prime number.  For example, it might look like this:
Please enter a number terminated by a carriage return:
100001

The number:
100001
is NOT prime.  The next prime number is:
100003


Please enter a number terminated by a carriage return:
12347

The number:
12347
is prime.


Please enter a number terminated by a carriage return:

 ...etc

Here is a list of some prime numbers with which to test your code.

1000003 1000033 1000037 1000039 1000081 1000099 1000117 1000121
1000133 1000151 1000159 1000171 1000183 1000187 1000193 1000199
1000211 1000213 1000231 1000249 1000253 1000273 1000289 1000291

100000000000000000039 100000000000000000129 100000000000000000151
100000000000000000193 100000000000000000207 100000000000000000301
100000000000000000349 100000000000000000361 100000000000000000391
100000000000000000393 100000000000000000441 100000000000000000477
100000000000000000547 100000000000000000559 100000000000000000561
100000000000000000721 100000000000000000741 100000000000000000753
100000000000000000757 100000000000000000763 100000000000000000801
100000000000000000853 100000000000000000961 100000000000000000993

Optional experimental exercise

For those of you who wish to do a little experimentation:

For each of a collection of randomly chosen non-prime integers X having between 20 and 40 digits, and having no prime factor with value less than 200, select 1000 witness candidates b randomly chosen from the set (1...X-1) and see how many of them are witnesses to X's compositeness as determined by your primality tester.
The theorem we discussed in class states that there is an expectation that at least half of the candidates will be witnesses.  Was this an overly conservative expectation?
Based on your observations, how many iterations of the main loop should be performed to enable stating that a number is prime with probability of error less than one in a trillion?


Last modified: Apr 11, 2011