CS108 Lab 5: Prime Numbers and Hypothesis Testing

If/else blocks can be useful independently, but many uses also involve a loop. In this case, use the "for each number" loop to make a prime number detector. Add a textbox, a label, and a button. The textbox is for the user to enter a number, which can be checked to see if it is prime. The button should be named "Check" or something similar. The label will display results. Set up logic like this for when the button is clicked:
ResultLabel.text = "Prime"
for each number from 2 to EntryBox.text - 1 by 1
do
	if modulo of EntryBox.text % number == 0
	then
		ResultLabel.text = "Divisible by " + number
	end of if/else block
end of for block
	
Modulo or modulus is the remainder when dividing. So, 9 divided by 7 is 1 with a remainder of 2, so the modulus is 2. It is normally represented by the % sign, so 9%7 = 2. A number is prime if it is only divisible by 1 and itself. If it is divisible by a number, than the modulus when dividing by that number will be 0 (no remainder). So we check every number from 2 up to and including one less than what we are testing, and if it is not divisible by any of those numbers, than it is prime. Setting the label to "Prime" at the beginning allows two possible outcomes:
  1. The contents of the entry box are not divisible by any of the numbers we checked, and the label text "Prime" is not changed. Since it isn't changed, the user is left seeing the text "Prime".
  2. The contents of the entry box are divisible by some number(s) we test. In this case, the label text is changed (possibly multiple times), and the user is left seeing the last update (the largest number that the value in the textbox is divisible by).
Once the app can accurately determine if numbers are prime, the lab is complete.

Optimizations

This program is somewhat slow if presented with a large number to check. Several optimizations are possible. The first is to treat dividing by 2 as a special case, and use the loop for numbers from 3 on by twos. This requires an early test to see if the textbox contains an even number, but can cut the number of checks by half. Similar optimization for 3, 5, etc. is possible but becomes more complicated. This optimization (checking for divisibility by 2) will be required for the hypothesis testing section of the lab below, but make sure to record runtimes without the optimization before adding it.
The second optimization is only for numbers which are not prime, and requires a control construct called "break", present in most languages. I haven't found it in App Inventor, but it works like this:
ResultLabel.text = "Prime"
for each number from 2 to EntryBox.text - 1 by 1
do
	if modulo of EntryBox.text % number == 0
	then
		ResultLabel.text = "Divisible by " + number
		break
	end of if/else block
end of for block
	
The meaning of break above is to cease checking numbers and immediately move to the end of the loop block. So if checking a number such as 903, the loop would be terminated after it is determined that 903 is divisible by 3. In App Inventor, the loop can be converted to a "while" loop with a counter, so that early exit is possible.
The third optimization is to iterate from 2 through EntryBox.text / 2. For example, if checking to see if 991 is prime (it is), we can assume that 991 is not divisible by any number greater than 495, since any number that is more than half of 991 can't evenly divide 991. This would check prime numbers in about half the time. You can do better than this by using the square root of the given number, but make sure to save the result in a variable so that the square root won't be calculated each iteration.
Further more complicated optimizations are possible as well, but these three are the easy way to speed this process.

Hypothesis Testing

Consider the explanation of hypothesis testing given in lab, and test the following two hypothesis:
  1. The Prime Number Algorithm in Lab 4 scales in a linear manner
  2. Adding an optimization to eliminate even numbers from consideration before entering the main testing loop will improve scaling to O(1) for even numbers.

Testing Hypothesis 1

Add code to test using the clock. To do this:
  1. Add a clock to your Lab 4 app
  2. At the beginning of the prime test, save the number of milliseconds since 1970 in a variable
  3. At the end of the prime test, calculate the difference between the earlier reading, and the reading at this point
  4. Display the number for the user somewhere (add a label, or add more text to the existing label)
Test non-primes 10, 100, 1000. If the scaling is O(n), 1000 will take 10 times as long as 100, 100 will take 10 times as long as 10, etc. Try 10,000 as well. Besides non-primes, test primes of similar value, such as 11, 97, and 991. Put the numbers you find in your lab report, and your opinion on whether the hypothesis is supported. If you have an optimization in place for even numbers, disable it for these tests, and re-enable it for testing hypothesis 2.

Testing Hypothesis 2

Add a condition to test even numbers separately. If the number is divisible by 2, DO NOT enter the testing loop. You'll need to add an "if" statement for this. Test non-primes 10, 100, and 1000 again. Has the scaling changed? Put the numbers you obtain in your lab report, along with your opinion of whether or not the hypothesis is supported. If you already have this feature, you can comment it out, or drag it somewhere else for testing purposes. Be careful not to break it when you test the original algorithm.

How the Lab Report should Look

CS108 Lab Report

Hypothesis 1:  The Prime Number Algorithm from Lab 4 scales in a linear manner
Hypothesis 2:  Adding an optimization to eliminate even numbers from consideration before entering the main testing loop will improve scaling to O(1) for even numbers.

Initial Test Results:

10:    ## ms
100:   ## ms
1000:  ## ms

After optimization:

10:    ## ms
100:   ## ms
1000:  ## ms

Hypothesis 1:

Hypothesis 1 is supported / is not supported because ...

Hypothesis 2:

Hypothesis 2 is supported / is not supported because ...

Turning in the Lab

For this lab, besides the usual check-off system, also turn in a report explaining your results for both hypothesis tests.