4 ms·
solutions in python http://gist.github.com/617686 http://gist.github.com/617686 about 30 lines of code in the tersest style, 40ish in the readability-obsesssiv
by jiaaro 16y ago
solutions in python http://gist.github.com/617686 http://gist.github.com/617686
about 30 lines of code in the tersest style, 40ish in the readability-obsesssive style I prefer
- cube 16y agoThanks for sharing! Love the style and it's readability. My newbie python code did the trick, but is way harder to read - your code really showed me room for improvement!
- Natsu 16y agoI see that you did brute force primality testing, too. Am I the only one who at least only divided by odd numbers (and two) in their brute-force is_prime function? Technically, you only need to test divide by all primes less than or equal to sqrt(n), after all. I guess I could have used a faster primality test, but I didn't feel like writing anything that complex. I know there's a website out there that has a test that works for anything under about 10 billion, if memory serves, by using the probabalistic tests and doing a special check for the only exception. Heck, it even gives you the factors for the largest number in its range...
- jiaaro 16y agothis code solved the problem in under 1 second. I would say for code that is executed one time ever, even including the sqrt(n) part is premature optimization (though I did include it)