Monday, November 27, 2006

quick mod by 11

1==1 (mod 11)
10==-1 (mod 11)
100==1 (mod 11)
1000==-1 (mod 11)

Therefore 123456 == 6 - 5 + 4 - 3 + 2 - 1 (mod 11) == 3 (mod 11)
Obviously you may need to do a further % operation after adding and subtracting digits, but it should be reasonably fast.

3 comments: