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

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

English version

Find all prime numbers between N and M, with 0 < N < M < 1025.

A further classification said that the time limit is 1 second and M - N < 109.

I'm wondering if there is a solution for this problem.

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

»
9 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

I'm not sure but I don't think there is a solution to this problem for 3 reasons.
1-) It is obvious that we can't iterate over those M-N numbers. It would be 5 * 108 by itself which would exceed 1 sec. run time limit.
2-) Since the numbers are too large we can't use prime sieves either, at least the ones I know.
3-) Even though we find the prime numbers in 0 sec, I assume we won't be able to print them out in 1 sec since there are probably more than 107 prime numbers between N and M in the worst case.
It would be great if someone can find a solution though.

  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    And the main problem is to find prime numbers between 2 strings because of large numbers

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

maybe BigInteger.nextProbablePrime() in java will work? not sure, though.

  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    But we should use Pascal

    • »
      »
      »
      9 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      The algorithm behind is Miller-Rabin primality test , so you need to implement it yourself

      • »
        »
        »
        »
        9 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Miller Rabin will probably be too slow. By the Prime Number Theorem, primes exist below N, and Miller Rabin runs in or something. Even assuming k=1, this means it will take O(N) where N = 1e9. I think you have to find each prime in constant time somehow.

  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Well, it would definitely speed up the process but unfortunately not enough.
    With that function or the algorithm behind it, we still need to check primality of the returned numbers. That means the time complexity is O(primalitytestcomplexity * 107) in the best case when M - N = 109. I don't think there is a primality test with a time complexity of less than O(log2N)

»
9 лет назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

http://www.wolframalpha.com/input/?i=prime+numbers+less+than+10^9

There are 50000000+ primes under 10^9 :( simply printing these will be a big challenge.

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Actually the 2nd and 3rd problems in this paper were very easy, so I am quite sure that this problem was having some mistakes, or the actual tests' limits are small enough to brute force. Anyway this is the Vietnamese problems' style nowadays.

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

Maybe use range sieve. It will require about sqrt(M) + (M-N) operations and bits of memory, even with inf memory this will take >> than 1 sec

»
9 лет назад, # |
  Проголосовать: нравится +37 Проголосовать: не нравится

Usually the trick to do well in middle school level competitions in Vietnam is to just ignore the unrealistic constraints.

  • »
    »
    8 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Wow, please explain to me why this problem it's too big limits?

    • »
      »
      »
      8 лет назад, # ^ |
        Проголосовать: нравится +6 Проголосовать: не нравится

      koosaga has already said: There are 50000000+ primes under 10^9 :( simply printing these will be a big challenge.

      • »
        »
        »
        »
        8 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится
        • How this problem can be resolved?
        • As always, Maybe a big limit is a good chance to find good student?
        • »
          »
          »
          »
          »
          8 лет назад, # ^ |
            Проголосовать: нравится +1 Проголосовать: не нравится

          This problem is (probably) impossible to be resolve.

          As you can see, I wrote a program 24418001 to print integer from 1 to 50000000 here and submit to a random problem and got TLE(more than 2 second while the time limit here is 1 second).

          I agree that big limit is a good chance to find good student, but giving task that is practically impossible is not the same.

          • »
            »
            »
            »
            »
            »
            8 лет назад, # ^ |
              Проголосовать: нравится +1 Проголосовать: не нравится
            • Exactly, I agree with your point of view too...
            • But with my opinion, such as age, Such a request is too much...
    • »
      »
      »
      8 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Long long time ago, the limit of problems wasn't big as present... but nowadays the limit is bigger, bigger after each year. But I think that a 9-grade student doesn't think about Miller-Rabin or some complex algorithms. Maybe a big limit is a good chance to find good student?

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

this problem is not hard , just u r stupid