The first function isPrime()
does what it is supposed to. Returns true if a number is prime and returns false if it is not. The reason why the loop variable a
runs only till n/2
is because any number n
cannot have a factor that is greater than n/2
(apart from itself). EXAMPLES? 6 -- 1, 2, 3 and 6
, 12 -- 1, 2, 3, 4, 6 and 12
. The loop is just trying to see if a
has any factors (numbers that divide it without leaving a remainder). If it does it is not a prime (return false
) else it is (return true
).
However I feel that primeCount()
doesn't completely do what it is intended to.
From the definition of primeCount()
I think it is meant to compute the total number of prime numbers from x
to y
(in your case 2 to 50000, since you mentioned main()
calls primeCount(2, 50000)
). However for it to do this the for
loop must be changed to this
for (b = x; b <= y; b++)
The role of the variable c
here is to keep a check for every thousandth value of the loop variable b
.
Notice that on 1st run when b = 1000
i.e b == c
the program prints the number of primes that it encountered until now (counter
). After that the counter
is reset to 0
and c
is now 2000
.Then, b
goes on from 1001
to 2000
and the same thing repeats till b
is 50000.
Overall, The idea is to print the number of primes that exist within each 1000 natural numbers from 2
to 50000
.