Prime Numbers Help

Hi,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
// note the use of function prototypes

bool isDivisible (int number, int divisor);
bool isPrime (int number);

using namespace std;

int main ()
{
    for ( int i = 0; i < 100; i++ )
    {
        if ( isPrime( i ) )
        {
            cout << i << endl;
        }
    }
}

bool isPrime (int number)
{
for ( int i = 2; i < number; i++)
{
    if ( isDivisible( number, i ) )
     {
        return false;
     }
}
    return true;
}

bool isDivisible (int number, int divisor)
{
    return number % divisor == 0;
}


I'm not sure why the author is doing a for loop from i = 2 to i < number and I'm not sure what the isDivisible function does exactly.

Could someone explain it clearer? Thanks.
What is the definition of a prime number? Yes, math proves that one does not have to test them all, but that loop is the standard naive solution.


What you can tell about the isDivisible?
The loop in another view would look like this

while i < 100;
do something
i=i+1;

If your asking why i=2, well we don't need to check if 0 or 1 is prime....
0 cant' be divided and 1 can only be divided by itself.
Last edited on
Topic archived. No new replies allowed.