Prime Numbers A Prime number is any number greater than 1 that

advertisement
Prime Numbers
A Prime number is any number greater than 1 that can be evenly divided (no remainder) only by itself and 1.
Here are a few examples: 2, 3, 5, 7. The numbers 4, 6, 8, and 9 are not Prime numbers because they can be
evenly divided by 2 or in the case of 9, by 3.
Can you write a program to calculate every Prime number from 2 to 5000?
Create a form like this:
When the Calculate button is clicked, find the Prime numbers up to 5000.
Add them to the text box or a label.
Here are some tips to help you.
You know from class that you can have code inside of code, like an If statement inside another If statement.
You can also have a For loop inside of another For loop. You could have the outside loop go from 2 to 5000.
The loop variable for this outside loop is the number being checked to see if it is Prime. The inside loop could
go from 2 to the outside loop variable minus 1. Why stop at the outside loop value? Because if the inside loop
variable is bigger than the outside loop variable then the number will not divide evenly. Why minus 1?
Because a Prime may be divided by itself so why check it?
You might want to have a variable that keeps track of whether an outside loop number is Prime or not. You
could set it to True before the inside loop, then change it to False if conditions say it is not Prime.
Inside the second loop you can check to see if the potential Prime number divided by the inside loop variable
divides evenly, or has a remainder.
Suppose you have numbers represented by X and Y. How do you know if X / Y has a remainder? There are
several ways. Since we are trying to test your skill with loops, not your skill with math, we’ll just give you one.
The easiest way is with the Mod operator. Assuming you have defined a variable called Remainder:
Remainder = X Mod Y
After your loop, you can add the Prime number to the textbox or label if the variable keeping track is still True.
You will probably want some spaces between the Primes.
Download