Блог пользователя Funfunfunfun

Автор Funfunfunfun, история, 5 лет назад, По-английски

The other day I met a problem that needed to count the number of primes less than n with n <= 1e10. But I only know how to use sieve to solve but it can only run with n <= 1e6. Can you help me find a better solution? thanks for help!

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
 
Проголосовать: нравится -24 Проголосовать: не нравится

use segmented sieve, you can easily find videos of it on youtube

»
5 лет назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

I had the same curiosity a while ago, so I wrote some code to find pi(n) (which is what you need) and prime(n) (n-th prime number). You can play with N and X constants to see which gives better times. Here is the code. I used Meissel–Lehmer algorithm. You can read here and here about it. If you don't understand, don't be shy and ask me! Happy new year! :D