Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
lotd77757yLol.
Can't come up with anything where that's a good solution.
Unless it's on very low resources.. -
@lotd do some problems on projecteuler.net. Lots of them involve dealing with prime numbers.
I generate them myself in code, but generating the prime number list from 1 to 1 million takes around 2 seconds, and it's a O(n*log(n)) time complexity algorithm. Maybe to someone it does make sense to hardcode them. -
Player221317y@lotd yeah, it's what @sylflo said. If the point of the program isn't to generate them it is much quicker to hardcode them then to calculate them.
-
CptFox16167y@apisarenco do you need the whole list ? Because if you just need to generate random big primes, the Miller-Rabin test is your friend. Sure, it's probabilistic, but it's really fast and accurate enough that for very big numbers, you're more likely to get a wrong prime with the deterministic version due to EM caused error than to get a wrong one from the probabilistic one. Look it up if you haven't heard of it, pretty useful :)
-
CptFox16167y@apisarenco Well, Miller-Rabin can still be used, it is fast and reliable enough that you could just use it on every non-5-ending odd number and still iterate faster than the trivial prime generation anyways. Although I have mostly used it on huge numbers where trivial generation doesn't even apply. Maybe I should have a look at that project Euler thing. Heavy prime number usage can only mean fun.
-
@CptFox I just did an experiment:
I sieved all prime numbers from the range 2 - 1M, built a list out of them (process lasted 0.78s)
Then, I ran the Rabin-Miller test on every known prime number from that list (not a single test on a non-prime number), and that took 1.3 seconds.
Just for fun, after that I ran the primes test on every single number from that test, to try to find false positives. Well, no false positives found. But the process took 2.36 seconds, as opposed to 0.73 seconds that were necessary to build the data structure that identifies prime numbers up to 1M.
Then I decided to go funky, and did it with 10M numbers.
8.3 seconds for trivial sieve
25.12 seconds for every number verified with Rabin Miller test. Same ratio. No false positives. Rabin-Miller test is roughly 4 times slower, and scales the same.
But it's a good way to identify whether a single number is prime.
Oh... I did it on Python3, on Ubuntu 16.04, on a Core i7 4270HQ
Related Rants
I hope they used a program to generate that as output!
undefined
code
github
c++
prime numbers