0:02

Now, we're ready to generalize our steps.

Â I've written down the results of step two for both N equals 29,393 and N equals seven,

Â so that we can look for patterns across both of them.

Â Sometimes it helps us generalize to see multiple instances of the problem solved.

Â The first thing we might notice is that the numbers in

Â these two columns are always N. We check if 29,393,

Â is divisible by two through seven.

Â And we check if seven is divisible by two through six.

Â Logically, it make sense to always test N for divisibility so that we can update or

Â steps to reflect this generalization and replace these numbers with N. Next,

Â you might notice that these steps are now repetitive

Â except for these numbers which count from two to something.

Â And except for our answer which is sometimes no and sometimes yes.

Â If we get yes, as in N mod seven is zero,

Â then we immediately answer no.

Â And if we get no, as in all the rest of these checks,

Â we do nothing special.

Â So we can make these steps slightly more general,

Â check if N mod two is zero,

Â if so, answer no.

Â Check N and mod three is zero,

Â if so, answer no, and so forth.

Â Each time we check if N is divisible by a number,

Â if it is, we immediately answer no.

Â In the case of N equal seven after we tried all of these, we answered yes.

Â Here, you see counting.

Â The steps are completely repetitive except for these numbers that count.

Â On this side, we count two, three, four,

Â five,six which appears to be from two to N minus one.

Â Typically in computer programming,

Â we count the lower bound as inclusive and the upper bound as exclusive,

Â so we called this counting from two to N exclusive.

Â On the other side,

Â we seem to be counting from two to eight,

Â not including eight, which seems odd.

Â What does eight have to do with 29,393?

Â If we think about it a little more,

Â we see that we would count beyond this.

Â We just stopped here because we came across something that told us the answer was no.

Â That is, we would otherwise be counting from two to N as long

Â as we kept getting N not divisible by the number we're counting.

Â Now we can express this as a repetition of the same step,

Â count from two to N and to call each number that you count, i.

Â So i is two, three, four, et cetera,

Â then we take the step we're repeating and express it in terms of i,

Â check if N mod i is zero,

Â if so, answer no.

Â On the N equal seven side,

Â we have the same counting repetition except for one difference.

Â After we did all this counting, we answered yes.

Â So we're almost done,

Â but this have to look the same to be a general algorithm.

Â If they're different in some way,

Â we need to add conditions.

Â So what about this last step?

Â It turns out it's in there in general

Â after we finish counting and we haven't answered no.

Â So we can add it to the left side.

Â In the case of 29,393, the step was still

Â there but we never got to it because we stopped counting when we answered no.

Â Now, we're ready for step four.

Â We have a general algorithm we think but we may have generalized incorrectly.

Â It may be that there was something special about the two numbers we

Â picked and we didn't come across something that made it have other behavior.

Â So we want to test with values we haven't used

Â yet such as other yes answers like five and 13,

Â and other no answers like four and nine

Â to work through this and see if we're getting the right answer.

Â The more values we test,

Â the more confident we will be that this algorithm is correct.

Â We may also have missed some corner cases,

Â numbers for which our algorithm behaves strangely.

Â We might want to try some unusual values such as zero,

Â one, two or negative one.

Â Two is a good special case because we start counting at two.

Â Zero and one might be special because they're less than two,

Â so we wouldn't count any numbers.

Â Negative one might also be good since

Â we would count no numbers and immediately answer yes.

Â We do not however need to worry about things like 2.75 or the string,

Â "Hello world," because these are the wrong types.

Â We specified that N must be an integer and

Â the type system in our programming language like C is going to enforce this.

Â We won't be able to call this function with any argument that isn't an int.

Â Now that you've tested these out,

Â you've seen that the algorithm gives the correct answer for five, 13, nine, and two.

Â However, it gives the incorrect answer for zero,

Â one and negative one.

Â It won't count any numbers so we will answer yes even though these are not prime numbers.

Â To fix our algorithm,

Â we repeat steps one, two, three and four as needed.

Â In this case, there are no primes that are less than or equal to one,

Â so we add to our algorithm.

Â Check if N is less than or equal to one.

Â If so, answer no,

Â otherwise, we do all the other parts of the algorithm.

Â In the next video,

Â we'll show you how to translate this algorithm to code.

Â