division - check whether infinitely long binary number is divisible by 3 or not -


a infinitely long stream of 0's , 1's coming ,you have find out whether number formed far divisible 3 or not.

i tried finding decimal equivalent of binary number , summing digits , finding if sum divisible 3 or not. know wrong method because number infinitely long after time number out of range. approach this.

another approach find out positions set bits , odd positions set bits , if difference of total number of set bits odd , positions divisible 3 number divisible 3. here after sum time number out of range.

is there better approach?

just keep track of x = current number mod 3.

if b next bit, update:

x = (2*x + b) % 3

when x = 0 current number divisible 3


Comments

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -