**First part:** The isprime method uses an if-statement, with a modulo division, to weed out even numbers. It special-cases 2 (a prime).

**For, range:** This part loops, starting at 3, through all possible factors. It increments by 2, skipping evens.

**Square:** In the for-loop, we eliminate a possible factor "i" if its square is more than the candidate number. This speeds things up.

**Tip:** Memoization is a good optimization to apply to a method like isprime above.

**Also:** Another option is to generate a list or dictionary of known primes and reuse it.

Home

Dot Net Perls

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.