TITLE: Divisibility by 7 of a number consisting of 0 and 1's
QUESTION [5 upvotes]: A decimal number is of arbitrary length consisting of 0 and 1 only  i.e. (10,111,10000,11111,1010, number of digits in the number can be upto 100 )
Can this number ever be divisible by 7  if yes, is there any efficient way to list all those numbers

REPLY [2 votes]: $10 \equiv 3 \pmod 7$ and so $10^2 \equiv 2\pmod 7$ and $10^3 \equiv -1\pmod 7$ and $10^4 \equiv -3 \pmod 7$ and $10^5 \equiv -2 \pmod 7$ and $10^6 \equiv 1 \pmod 7$.
Let $N = 10^{a_n} + 10^{a_{n-1}} + ....... + 10^{a_0}$ where no $a_i = a_j$ if $i\ne j$ be a number consisting of only $0$s and $1$.
Then $N \equiv b_n + b_{n-1} + ...... + b_0 \pmod 7$ where 
where $b_k = \begin{cases} 1 &\text{if }a_k\equiv 0 \pmod 6\\ 3 &\text{if }a_k\equiv 1 \pmod 6\\ 2 &\text{if }a_k\equiv 2 \pmod 6\\ -1 &\text{if }a_k\equiv 3 \pmod 6\\ -3 &\text{if }a_k\equiv 4 \pmod 6\\ -2 &\text{if }a_k\equiv 5 \pmod 6\\ \end{cases}$
So Choose a bunch of $a_k$ where the sum of the $b_k$ add to $0$ or a multiple of $7$.
Example If $a_0 =0$ then $b_0=1$ and so if $a_1=3$ then $b_1=-1$ and so $10^{a_1} + 10^{a_0} =10^3+ 10^0 = 1001$ will have be divisible by $7$.  And indeed $1001 = 7*143$.
Another example if $a_0=1$ and $a_1=2$ then $b_0=3$ and $b_1=2$ so $b_0 + b_1=5$ so we need the rest to and to $-5$ or $2$ or $2$ more than  multiple of $7$. 
So we could do $a_2=4$ and $a_3=5$ to get $b_2=-3; b_3=-5$ and so $10^1 + 10^2 + 10^4 + 10^5 = 110110$ is divisible by $7$.
Or we could to $a_3=2+6=8$ and $b_3=2$ so $10^1 + 10^2 + 10^8 = 100000110$ is divisible by $7$.
Of course if you get any $N$ divisible by $7$ then $10^k*N$ will also be divisible by $7$.