CS 312: Algorithm Analysis

Project #1: Primality Test


The purpose of this project is to get you up and running with VisualStudio and to familiarize you somewhat with the tools. You will also gain experience with algorithms for modular arithmetic. In this project, you will implement the prime number test based on Fermat's little theorem (see pseudocode on page 27, figure 1.8 of the text book).



1.   Launch VisualStudio and start a new windows C# project. (Project type should be set to "Visual C#" then select the "Windows Forms Application" template).

2.   Create a form with three textboxes and a button. (Buttons and textboxes can be selected from the toolbox in the form designer. If the toolbox is not visible, click on "View | Toolbox").

3.   Name one textbox "input" one "output", and the other is for the k value (you will enter an arbitrary k value as described in the book).  (right click on the textbox to view properties. One of the properties is the name. The default name will be textBox1 or something similar).

4.   Name the button "solve" and change the text label to "solve" (again, right click on the button to set the properties. The text label field is called "text").

5.   Next we want to create the code that will be executed when the solve button is clicked. To do this, double-click on the solve button in the form designer. Doing so will create a stub for the method that will be called when the button is clicked. You can then type your code into that stub.

6.   You should now be editing code in a method called solve_click() in a file called "Form1.cs"

7.   Code up the primality test pseudocode from figure 1.8 of the text.  You may assume that the input is less than 1024 and set k to any value you like (see p. 27). Be sure to also implement modular exponentiation (pseudo-code in figure 1.4 on p. 19).  Your primality test should use your modular exponentiation function.

The contents of the input textbox can be accessed as


and a string can be converted to an integer using


You can set the value of the text in a textbox in much the same way:

                   output.Text = "the output"

and similarly for the box where you enter in the k value you want to use for the current test.

8.   Be sure to avoid re-using the same random values for your repeated tests.

9.   If Fermat's test returns "yes" then write "yes with probability p" (where p is the probability that the input was prime) in the output textbox.  If Fermat's test returns "no" then write "no" in the output textbox.



A report consisting of 3 sections:

1.        [20 points] At least one screenshot of your application with a working example

a.        Note: to capture just the top window in your screenshot, use the key-chord ctrl-shift-alt-PrtSc (i.e., print screen)

2.        All of the code that you wrote. In most cases, the code will be the contents of the Form1.cs file.  Your code must include the following:

a.        [30 points] A correct implementation of modular exponentiation.

b.        [30 points] A correct implementation of the Fermat primality tester.

c.        [10 points] The remainder of your code

3.        [10 points] The equation you used to compute the probability of correctness p that appears in the output.


Note that there is no performance requirement for this project.  Correctness is the only criterion.

Submit as a PDF by following the submission directions in the course syllabus.


Last Updated: 2010 August 25