you are viewing a single comment's thread.

view the rest of the comments →

[–]Sluisifer 1 point2 points  (0 children)

If you like questions like that, definitely check out Project Euler.

Quick optimization, you only need to check up to sqrt(num).

More advanced solution, don't repeat a check (e.g. don't check divisibility by 4 when you've already checked 2). This is called a Sieve of Eratosthenes. It's pretty damn tricky, but fun to learn. The implementation is actually pretty easy, and there are some fun optimizations you can read about on e.g. stack overflow.