108

I hope they used a program to generate that as output!

Comments
  • 8
    Lol.

    Can't come up with anything where that's a good solution.

    Unless it's on very low resources..
  • 4
    @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.
  • 0
    @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.
  • 0
  • 1
    @Player2 figured that, but i only find it to be a good solution if it's running on very low resources..

    Be it low hardware specs or simply for the sake of it being lightweight.. :)
  • 2
    im pretty sure if you're gonna hard code prime numbers, you do it by hand
  • 1
    @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 :)
  • 0
    @CptFox you kinda need the full list most often.
  • 0
    @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.
  • 0
    @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
Add Comment