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.