Understanding Code

 

Slide 12

next »

This code (which could basically be C, C++, or Java) finds the number of primes less than the number n. It does this by checking every number from 2 to n (calling it i), and then checks every number between 2 and half of i to see if it divides i. If it does, prime is set to false. If prime is true at the end of the outer loop (i.e. no factors of i were found), the number of primes is incremented by 1. Finally, the number of primes found is returned.