8
^ 8 (mod 7) is 1, remember that
85
^ 1 · 10 (mod 7) is 3, and 3 + 5 (mod 7) is 1, remember that
853
^ 1 · 10 + 3 (mod 7) is 6
8536
^ 6 · 10 + 6 (mod 7) is 3
85362
^ 3 · 10 + 2 (mod 7) is 4
853629
^ 4 · 10 + 9 (mod 7) is 0
Hence 853629 (mod 7) = 0 and it is divisible by 7. The corresponding code would be def modulo(number, k):
val = 0;
for digit in str(number):
val = (val * 10 + int(digit)) % k
return val
Of course, you can apply the modulo within the intermediate calculations as well, so instead of multiplying by 10 you can e.g. multiply by 3 = 10 (mod 7) instead. The key idea is that you can go from one weight to the next by multiplying with 10 (mod 7), so if you sum some weighted digits you can update all the weights at once with a simple multiplication.The advantage is that you only need a small amount of memory (a single number between 0 and 6), so this is a kind of general divisibility rule where you only have to do small calculations in your head.
If I may digress a bit into automata theory, this means that you can construct a finite state automaton that determines whether a number is divisible by 7 (or any other constant) by reading its digits from left to right. Additionally, if a finite state automaton can do something reading from left to right, there is another automaton doing the same thing but reading from right to left. This means that there is also a 'simple' divisibility rule when starting with the least significant digit, though it may be a bit harder to find.