Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

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

Автор red_coder, 12 лет назад, По-английски

suppose we want to find a number that starts with N and is divisible by 2520. I found this statement written 'note that there is such number somewhere between N0000 and N2519, inclusive'. Now my question is HOW???????

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

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

the answer is N*10000+2520-((N*10000)%2520)

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

    if we consider N= 5040 then the answer is N itself(5040) coz it starts with N and is divisible by 2520. but this number is not between N0000 and N2519??????

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

      If N is divisible by 2520, result is N0000; at any cases the above formula is right.

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

    seems it will fail for N=2520 or multiples of 2520. 2520*10000 + 2520 — ( (2520*10000) % 2520) = 25202520 which is not between N0000 and N2519

    to fix it: N*10000+ ( 2520-((N*10000)%2520) ) % 2520

    or: N*10000 + 2519 — (N*10000-1)%2520 (assuming N>0)

    is there any better way to think about modulos to avoid this kind of mistake?

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

      actually i am also confused about this thing... according to me Kostroma is correct.. the answer is N*10000+2520-((N*10000)%2520) but then the result is failing for N=2520.. i am too much confused :O

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

        You want to find a number X such that:

        (N*10000 + X) mod 2520 = 0

        , where X>=0 and X<=2519

        From Kostroma's formula, X = 2520-((N*10000)%2520)

        But Kostroma's formula will give you a value of X>=1 and X<=2520 which satisfies the equation. So there is a corner case when X=2520 and you need to fix it back to X=0. By taking modulo again (X=X%2520) or just doing a simple check if (X==2520) X=0; , you can fix it.

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

We want to find a consistent number, not the smallest one, so the set of remainders of [N0000,N2519] when dividing by 2520 will always be [0,2519] (in some order, not necessarily corresponding).

The formula of Kostroma is almost correct, except when N is divisible by 2520, it should be N0000 instead.