Why is Sieve of Eratosthenes more efficient than the simple “dumb” algorithm?

后端 未结 7 2019
遇见更好的自我
遇见更好的自我 2020-12-08 21:56

If you need to generate primes from 1 to N, the \"dumb\" way to do it would be to iterate through all the numbers from 2 to N and check if the numbers are divisable by any p

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 22:12

    http://en.wikipedia.org/wiki/Prime_number#Number_of_prime_numbers_below_a_given_number

    • the "dumb" algorithm does i/log(i) ~= N/log(N) work for each prime number
    • the real algorithm does N/i ~= 1 work for each prime number

    Multiply by roughly N/log(N) prime numbers.

提交回复
热议问题