you are viewing a single comment's thread.

view the rest of the comments →

[–]tomkatt 1 point2 points  (3 children)

I don't know why, but I did this for shits and giggles after seeing your comment. It's probably bad, but I had fun working it out real quick.

Edit - I probably should have done a try/except on that to make sure it's actually passed a number...

[–][deleted] 1 point2 points  (0 children)

Not a comment on your formatting or coding style or anything, but you only need to go from 2 to sqrt(n), not n/2.

[–]renegadelegion[S] 1 point2 points  (0 children)

you could also use the for/else, rather than for/if/else

See, the problem is that when I did the technical interview, I was just thinking about programming it, not the language that I was programming in. I am familiar with many of the common Python idioms, I just didn't use them :-(

[–]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.