Hacker News new | past | comments | ask | show | jobs | submit login

I 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...




this 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)




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: