Hacker News new | past | comments | ask | show | jobs | submit login

You asked "how does it work?" so let me answer that. How it works:

Okay, so the black arrows represent adding one, and the white arrows represent multiplying by ten. The circle that you see represents the fact that after we hit 7 when adding by one, we "loop around" to 0 again, because 7 has remainder 0 when divided by 7.

So we are constructing (in their example) 325 by saying:

    Instruction     | Number | Remainder
    start with 0.   |    0   |  0
    add 3.          |    3   |  3
    multiply by 10. |   30   |  2
    add 2.          |   32   |  4
    multiply by 10. |  320   |  5
    add 5.          |  325   |  3
Now the only thing you really need to know is that "multiply by 10" is only a function of the current remainder, it can never depend on the rest of the digits we've processed before in any other way.

To see this, just write out the number. The number is 7 k + r for some remainder r. Multiplying by 10 produces 70k + 10 r. Taking the remainder when you divide by 7 is just... (10 r) % 7, because the first term is divisible by 7. So you only need to keep track of the remainder.

In fact, 10 r is really 7r + 3r, and the 7r is also divisible by 7, which means that the white arrows really just multiply by 3. That is why 0 maps to 0, 1 maps to 3, 2 maps to 6, and 3 maps to 9 % 7 == 2.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: