TITLE: Why the sum of $n$ seems equals the period of the binary expansion of $1/n$?
QUESTION [11 upvotes]: The sum of $n$(suppose $n$ is positive odd,using $n=23$ as an example):
Step 1 : Get the odd part of  $23 + ~~1 $, which is $~~3$,$~~3\times2^3=23 + ~~ 1$,get $s_1 = 3$
  Step 2 : Get the odd part of  $23 + ~~3 $, which is $13$,$13\times2^1=23 + ~~ 3$,get $s_2 = 1$
  Step 3 : Get the odd part of  $23 +  13 $, which is $~~9$,$~~9\times2^2=23 +  13$,get $s_3 = 2$
  Step 4 : Get the odd part of  $23 +  ~~9 $, which is $~~1$,$~~1\times2^5=23 +  ~~9$,get $s_4 = 5$ 
Continuing this operation (with $23 + 1$) repeats the same steps as above. There are $4$ steps in the cycle, so the cycle length of $23$ is $4$，and the sum of $23$ is $s_1 + s_2 + s_3 + s_4 = 11$.
The period of the binary expansion of $1/23$ is $11$,so why the sum of $23$ seems equals the period of the binary expansion of $1/23$?

REPLY [2 votes]: Something more general is true.
Let $b\geq 2$ and and $n$ any positive integer coprime with $b$. Define the sequence $s_i$ as follows:

The first term $s_1$ is defined as the smallest non negative integer such that $b^{s_1}t_1=n+1$ and $b\not\mid t_2$.
  The second term $s_2$ as the smallest non negative integer such that $b^{s_2}t_2=n+t_1$ and $b\not\mid t_2$.
  The third term $s_3$ as the smallest non negative integer such that $b^{s_3}t_3=n+t_2$ and $b\not\mid t_3$.
  $\hspace{38 ex} \vdots$
  Finally the $d^{th}$ term $s_d$ as the smallest non negative integer such that $b^{s_d}=n+t_{d-1}$.

That this sequence is purely periodic, is explained here. What you observed is that the sum of the $s_i$ is the cycle length of $1\over n$ written in base $b$. But, that cycle length of $1\over n$ is the order of $b\pmod n$ since both these numbers can be described as the smallest positive integer $s$ such that $b^s {1\over n}-{1\over n}\in\mathbb N$.
Therefore, it remains to show that $s=s_1+s_2+\ldots+s_d$ is the order of $b\pmod n$ which it's easy to see that it is equivalent to showing that $\operatorname{ord}_n\left(\dfrac1b\right)=s$ or that $b^s=1$ and $\dfrac{1}{b^i}\neq1$ for all $i=1,2,\ldots,s-1$. 
Set $t_0=t_{d+1}=1$. Reducing the equations $b^{s_i}t_i=n+t_{i-1}\pmod n$, we obtain $b^{s_i}t_i=t_{i-1}\pmod n$ and therefore $b^s=1\pmod n$. Now for the second part note that 
$$
\dfrac1b=b^{s_1-1}t_1\pmod n, \ \dfrac1{b^2}=b^{s_1-2}t_1\pmod n,\ldots,\dfrac1{b^{s_1}}=t_1\pmod n,
\\
\dfrac1{b^{s_1+1}}=\dfrac{1}{b}t_1=\dfrac{1}{b}b^{s_2}t_2\pmod n=b^{s_2-1}t_2\pmod n\ldots$$
It's not so hard to show that all these values are different from $1\pmod n$. 
You have to do some work. It may happen that some of the $s_i$ are zero and therefore you have to consider several cases. The case $b=2$ is of course easier since from the fact that $\gcd(n,t_i)=1, \ \forall i$ (why?) it follows that $s_i\neq0 \ \forall i$.
Therefore $\operatorname{ord}_n\left(\dfrac1b\right)=\operatorname{ord}_n(b)=s$.