Hello to everyone, i recently started to learn c++ with this book "Jumping into C++" from Alex Allain. In this book there is an example of a program to get the prime numbers from 0 to 100 that Im not understanding!
For what I think I understood the second loop of the function "isprime" is going to try to divide from 2 to the number generated by the first loop in function "main" with the modulus operator from the function "isDivisible", a prime number is a number that only as its self and 1 as divisors, but how can the modulus operator "see"
what numbers have only them selfs and 1 as divisors?
If someone could explain the steps of this program to me I would be very grateful.

Heres the code:

#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;
} 
}
system("pause");
} 
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;
} 

Edited 3 Years Ago by guilherme.carvalho.9250

The modulus operator's role is to return the remainder (leftover) from integer division. For example, 17 / 5 gives a quotient of 3. What about the remainder, 2? That we can get with modulus. 17 % 5 gives 2.

When a number divides fully into another, modulus gives a value of 0, there was no remainder. So the isDivisible function returns either a true when the divisor goes into the numerator completely, and returns false if there was any remainder.

The isDivisible function is called many times, it does not "see" anything about the number being tested. All it is doing is testing a specific divisor at any one instance. The isPrime function tests the input number for all possible divisors, if any are found, that determines the number is not prime.

Note: this code, which is a simple but not necessarily the most efficient method, could be improved by not wasting time on impossible divisors.
Line 15 should be (at a minimum) modified as:

for ( int i = 2; i < number/2; i++) //don't test divisors more than half the size

Hello thanks for replying, and explaining the program but I m still not understanding.What do you mean by "goes into the numerator completely"? For example, 10%2 and 10%5 the remainder is 0 and only 3%3 gives 0!The program is determining prime numbers by the remainder of the divisions giving 0 right?How does the program knows that 10 isnt prime and 3 is?Because both give remainder 0! THats whats making me confuse.

Walk through it.
Consider 3.
isPrime tests 3 % 2 there's a remainder of 1, so 2 does not go into 3 evenly. You won't test 3 % 3, the loop stops before that. It's prime,

Consider 4 % 2 the remainder is 0, so 2 is a factor of 4. It's not prime, test stops

Consider 5. 5 % 2 gives remainder 1, it's still a candidate.
5 % 3 gives remainder 2, it's still a candidate
5 % 4 gives remainder 1, and we have no more tests. It's prime.

This question has already been answered. Start a new discussion instead.