Group Abstract Group Abstract

Message Boards Message Boards

Formula for computing sqrt(2) of binary numbers

Attachments:
POSTED BY: Mariusz Iwaniuk
14 Replies

enter image description here -- you have earned Featured Contributor Badge enter image description here Your exceptional post has been selected for our editorial column Staff Picks http://wolfr.am/StaffPicks and Your Profile is now distinguished by a Featured Contributor Badge and is displayed on the Featured Contributor Board. Thank you!

POSTED BY: EDITORIAL BOARD

Hello, here is one I have found recently:

The n'th binary digit of sqrt(2) is given by

floor(Sum_{n>=1} A005875(n)/exp(Pi*n/(2^((2/3)*k+(1/3))))) mod 2

Will give the k-th binary digit of sqrt(2), where A005875 is the number of ways to write n as sum of 3 squares. See image of the formula : r_3(n) is A005875.
enter image description here

NOTE : there is one for sqrt(10) also which is

enter image description here

NOTE 2: The series converge quite slowly but there is a marker for the decimal point which enables to get the n'th. The actual precision is very high when k >> 1.

POSTED BY: Simon Plouffe

I found this very interesting so I tried to find a similar formula for the n-th digit of $\sqrt2$ in decimal. For that I used the following, already existing formula for the decimal expansion of $\sqrt2$ (from http://oeis.org/A002193):

$a(n)=-10 \cdot \lfloor 2^{-\frac{3}{2}+n} \cdot 5^{-2+n}\rfloor + \lfloor 2^{-\frac{1}{2}+n} \cdot 5^{-1+n} \rfloor$,

applying this on it:

$\lfloor x\rfloor =x-\frac{1}{2}+\frac{\sum _{k=1}^{\infty } \frac{\sin (2 k \pi x)}{k}}{\pi }$

and then following the same steps you did for base 2. This way I got the following formula:

$B_{10}(n) = \frac{9}{2}-\frac{10 \tan ^{-1}\left(\cot \left(\sqrt{2} \pi10^{n-2} \right)\right)}{\pi }+\frac{\tan ^{-1}\left(\cot \left(\sqrt{2} \pi 10^{n-1} \right)\right)}{\pi }$,

which was working as expected. At this point I had a suspicion for a generalized formula for the n-th digit in any base $b \geq 2$ :

$B_{b}(n) = \frac{b-1}{2}-\frac{b \tan ^{-1}\left(\cot \left(\sqrt{2} \pi \cdot b^{n-2} \right)\right)}{\pi }+\frac{\tan ^{-1}\left(\cot \left(\sqrt{2} \pi \cdot b^{n-1} \right)\right)}{\pi }$

which is the formula you found for $b=2$ and the one I found for $b=10$ and I also tried $b=16$ for Hexadecimal and it returned correct values. But at this point I had another suspicion, namely that you could generalize it so that you get the b-th base expansion for any positive real number $0 < u \in \mathbb{R}$:

$B_{b}(n) = \frac{b-1}{2}-\frac{b \tan ^{-1}\left(\cot \left( u \cdot \pi \cdot b^{n-2} \right)\right)}{\pi }+\frac{\tan ^{-1}\left(\cot \left(u \cdot \pi \cdot b^{n-1} \right)\right)}{\pi }$,

and indeed, this formula could return for example the binary and decimal expansions of $u=\pi$ or $u=\frac{1}{9}$. I am not sure how useful these formulae would be though because you need the value of $u$ to calculate the n-th digit with it.

Try, where u is Pi:

F[n_, b_, u_] := Floor[b^(-1 + n) *u] - b*Floor[b^(-2 + n) *u] ;

{Table[F[n, 10, ((2^(2 + 4 n) (1 - 3^(-1 - 4 n)) (4 n)!)/
EulerE[4 n])^(1/(1 + 4 n))], {n, 1, 50}], mm = N[Pi, 50];  
RealDigits[mm, 10][[1]]} // MatrixForm
(*Pi for base = 10 *)

{Table[F[n, 2, ((2^(2 + 4 n) (1 - 3^(-1 - 4 n)) (4 n)!)/
EulerE[4 n])^(1/(1 + 4 n))], {n, 0, 55}], mm = N[Pi, 17]; 
RealDigits[mm, 2][[1]]} // MatrixForm 
(*Pi for  base = 2 *)

Formula the n-th digit off Pi for base=b:

$B_b(n)=\left\lfloor b^{-1+n} \left(\frac{2^{2+4 n} \left(1-3^{-1-4 n}\right) (4 n)!}{E_{4 n}}\right){}^{\frac{1}{1+4 n}}\right\rfloor -b \left\lfloor b^{-2+n} \left(\frac{2^{2+4 n} \left(1-3^{-1-4 n}\right) (4 n)!}{E_{4 n}}\right){}^{\frac{1}{1+4 n}}\right\rfloor$

Where: EulerE[4 n] are Euler numbers.

   f[n_] := I Sum[Binomial[k, l] ((-1)^l*(k - 2 l)^(4 n + 1))/(2^k*I^k*k), {k, 1,4 n + 1}, {l, 0, k}]
   Table[{EulerE[4*n], f[n]}, {n, 1, 5}] // MatrixForm

Regards M.I

POSTED BY: Mariusz Iwaniuk

Hey, while trying to simplify the equation further I was able to get this:

The $n$-th digit in base $b$ of a number $u$ can be calculated as:

$$ B_b(n):=\frac{b-1}{2}-\frac{b\cdot\tan^{-1}(\cot(u\cdot\pi\cdot b^{n-1}))}{\pi}+\frac{\tan^{-1}(\cot(u\cdot\pi\cdot b^{n}))}{\pi} $$

Given that $\tan^{-1}(\cot(x))=\frac{\pi}{2}-(x \mod \pi)$ this can be simplified to: $$ B_b(n):=\frac{b-1}{2}-\frac{b\cdot(\frac{\pi}{2}-(u\cdot\pi\cdot b^{n-1}\mod \pi))}{\pi}+\frac{\frac{\pi}{2}-(u\cdot\pi\cdot b^{n}\mod \pi)}{\pi} $$

Given that $\frac{x\mod a}{a}=\frac{x}{a}-\lfloor\frac{x}{a}\rfloor$ this can be further simplified to:

$$ \begin{align} B_b(n):&=\frac{b-1}{2}-b\cdot(\frac{1}{2}-(u\cdot b^{n-1}-\lfloor u\cdot b^{n-1}\rfloor)) + \frac{1}{2}-u\cdot b^{n}+\lfloor u\cdot b^{n}\rfloor \ &= \frac{b-1}{2}-\frac{b}{2}+b\cdot u\cdot b^{n-1}-b\cdot\lfloor u\cdot b^{n-1}\rfloor + \frac{1}{2}-u\cdot b^{n}+\lfloor u\cdot b^{n}\rfloor \ &= -b\cdot\lfloor u\cdot b^{n-1}\rfloor +\lfloor u\cdot b^{n}\rfloor \ &= \lfloor u\cdot b^{n}\rfloor -b\cdot\lfloor u\cdot b^{n-1}\rfloor \ \end{align} $$

This one is nice and simple but I still don't think it would be useful

I am interested in proving that the proportion of binary digits of $\sqrt{2}$ is exactly $\frac{1}{2}$, and I have worked on this for a long time. You wrote a great article using the Fourier series expansion for the fractional part function. I am looking at a simpler formula (I have yet to find one!) but have asked my question on StackExchange, here. For your convenience, my question is posted below:

Is it possible to find an expression of the form $$\lfloor 2^n \sqrt{2}\rfloor = \sum_{k=1}^r (\alpha_k + \beta_ki)\cdot\Big(a_k+b_k i\Big)^n, $$ where $i^2 = -1$ and $\alpha_k, \beta_k, a_k, b_k$ real numbers? Maybe with $r< 5$ if possible. I am trying to compute the mean value (average) of $\{ 2^n \sqrt{2}\}= 2^n \sqrt{2} - \lfloor 2^n \sqrt{2}\rfloor$ over $n=0, 1,2, \cdots$. Let us denote as $p$ this mean value: $p$ is the proportion of binary digits of $\sqrt{2}$ equal to one, and $p$ is widely believed to be equal to $\frac{1}{2}$.

Note that $$p=\lim_{n\rightarrow\infty} p_n, \mbox{ with } p_n = \frac{1}{n}\sum_{k=1}^n \Big( 2^k \sqrt{2} - \lfloor 2^k \sqrt{2}\rfloor\Big).$$ Assuming we have a simple closed form for $\lfloor 2^k \sqrt{2}\rfloor$ as suggested in the first formula, then we can have a closed form for $p_n$, involving a finite number of terms. I don't expect this to be true if you replace $\sqrt{2}$ by (say) $\pi$ or $\log 2$, but I would expect this to work with any quadratic irrational.

Of course you can always use the Fourrier series to represent the fractional part function, but I don't think it would be easy to handle: it involves an infinite number of terms ( $r=\infty$.)

Finding formula for the n-th digit in the binary representation of Sqrt[2] using Floor function.

 sol = RSolve[{b[n + 2] == a[n + 1] - 2*a[n], a[n] == Floor[Sqrt[2]*2^n], b[0] == 1, b[1] == 0}, {b[n], a[n]}, n]
 sol2 = FullSimplify[b[n] /. sol[[1]], Assumptions -> {n > 0, n \[Element] Integers}]

$$B_n=-2 \left\lfloor 2^{-\frac{3}{2}+n}\right\rfloor +\left\lfloor 2^{-\frac{1}{2}+n}\right\rfloor$$

This is formula for the n-th digit in the binary representation of $\sqrt{2}$ for $n > 0$ and $n\in \mathbb{Z}$ using Floor function.

 Table[sol22 /. n -> m, {m, 1, 50}] 

$ {1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, \ 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, \ 1, 0, 0, 1} $

POSTED BY: Mariusz Iwaniuk

Just checking: is this, then, some kind of spigot algorithm for sqrt(2)?

https://en.wikipedia.org/wiki/Spigot_algorithm

POSTED BY: Sander Huisman

No is not algorithm, it's better than that. It simple, it's a formula, or function.

An example:

To start calculating binary digits from, say, the 8th place only need substitute for n=8,9,10,11,12,13,14,15....That it's all.

formula =1/2 - (2 ArcTan[Cot[2^(-(3/2) + n) \[Pi]]])/\[Pi] + ArcTan[Cot[2^(-(1/2) + n) \[Pi]]]/\[Pi]

sol = Table[formula, {n, 8, 15}] // N // Round

${1, 0, 0, 0, 0, 0, 1, 0}$

and say from n=1....15:

 sol2 = Table[formula, {n, 1, 15}] // N // Round

${1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0}$

POSTED BY: Mariusz Iwaniuk

I think it is then a spigot algorithm, because you have extensions, like the famous one for Pi in base 16 which calculates the nth digit directly... Algorithm here can be read as function:

> A further refinement is an algorithm which can compute a single arbitrary digit, without first computing the preceding digits: an example is the Bailey-Borwein-Plouffe formula, a digit extraction algorithm for ? which produces hexadecimal digits.

https://en.wikipedia.org/wiki/Spigot_algorithm

POSTED BY: Sander Huisman

You're right.So let it be.

Have a nice day. :)

Mariusz.

POSTED BY: Mariusz Iwaniuk

Appendix: To simplify $B_n$ formula.

We have: $$B_n=\frac{i \left(-i \pi +2 \log \left(1-e^{-i 2^{-\frac{1}{2}+n} \pi }\right)-2 \log \left(1-e^{i 2^{-\frac{1}{2}+n} \pi }\right)-\log \left(1-e^{-i 2^{\frac{1}{2}+n} \pi }\right)+\log \left(1-e^{i 2^{\frac{1}{2}+n} \pi }\right)\right)}{2 \pi }$$

 sol2 = (I (-I \[Pi] + 2 Log[1 - E^(-I 2^(-(1/2) + n) \[Pi])] - 2 Log[1 - E^(I 2^(-(1/2) + n) \[Pi])] - Log[1 - E^(-I 2^(1/2 + n) \[Pi])] +  Log[1 - E^(I 2^(1/2 + n) \[Pi])]))/(2 \[Pi])
 sol5 = FullSimplify[Re[sol2] // ComplexExpand, Assumptions -> n \[Element] Integers]
 Table[sol5, {n, 1, 5}]

$\left\{\frac{\pi -4 \tan ^{-1}\left(\frac{\sin \left(\sqrt{2} \pi \right)}{1-\cos \left(\sqrt{2} \pi \right)}\right)+2 \tan ^{-1}\left(\frac{\sin \left(2 \sqrt{2} \pi \right)}{1-\cos \left(2 \sqrt{2} \pi \right)}\right)}{2 \pi },\frac{\pi -4 \tan ^{-1}\left(\frac{\sin \left(2 \sqrt{2} \pi \right)}{1-\cos \left(2 \sqrt{2} \pi \right)}\right)+2 \tan ^{-1}\left(\frac{\sin \left(4 \sqrt{2} \pi \right)}{1-\cos \left(4 \sqrt{2} \pi \right)}\right)}{2 \pi },\frac{\pi -4 \tan ^{-1}\left(\frac{\sin \left(4 \sqrt{2} \pi \right)}{1-\cos \left(4 \sqrt{2} \pi \right)}\right)+2 \tan ^{-1}\left(\frac{\sin \left(8 \sqrt{2} \pi \right)}{1-\cos \left(8 \sqrt{2} \pi \right)}\right)}{2 \pi },\frac{\pi -4 \tan ^{-1}\left(\frac{\sin \left(8 \sqrt{2} \pi \right)}{1-\cos \left(8 \sqrt{2} \pi \right)}\right)+2 \tan ^{-1}\left(\frac{\sin \left(16 \sqrt{2} \pi \right)}{1-\cos \left(16 \sqrt{2} \pi \right)}\right)}{2 \pi },\frac{\pi -4 \tan ^{-1}\left(\frac{\sin \left(16 \sqrt{2} \pi \right)}{1-\cos \left(16 \sqrt{2} \pi \right)}\right)+2 \tan ^{-1}\left(\frac{\sin \left(32 \sqrt{2} \pi \right)}{1-\cos \left(32 \sqrt{2} \pi \right)}\right)}{2 \pi }\right\}$

Now we need find a pattern.The first ingredient in the formula: We have: $$\left\{4 \tan ^{-1}\left(\frac{\sin \left(\sqrt{2} \pi \right)}{1-\cos \left(\sqrt{2} \pi \right)}\right),4 \tan ^{-1}\left(\frac{\sin \left(2 \sqrt{2} \pi \right)}{1-\cos \left(2 \sqrt{2} \pi \right)}\right),4 \tan ^{-1}\left(\frac{\sin \left(4 \sqrt{2} \pi \right)}{1-\cos \left(4 \sqrt{2} \pi \right)}\right),4 \tan ^{-1}\left(\frac{\sin \left(8 \sqrt{2} \pi \right)}{1-\cos \left(8 \sqrt{2} \pi \right)}\right),\tan ^{-1}\left(\frac{\sin \left(16 \sqrt{2} \pi \right)}{1-\cos \left(16 \sqrt{2} \pi \right)}\right)\right\}$$

 FindSequenceFunction[{1, 2, 4, 8, 16}, n]
 (*2^(-1 + n)*)

Finding the second ingredient in the formula is the same how I found at first:

 FindSequenceFunction[{2, 4, 8, 16, 32}, n]
 (*2^n*)

Substitution:

$$\frac{\pi -\left(4 \tan ^{-1}\left(\frac{\sin \left(n \sqrt{2} \pi \right)}{1-\cos \left(n \sqrt{2} \pi \right)}\right)\text{/.}\, n\to 2^{n-1}\right)+\left(2 \tan ^{-1}\left(\frac{\sin \left(n \sqrt{2} \pi \right)}{1-\cos \left(n \sqrt{2} \pi \right)}\right)\text{/.}\, n\to 2^n\right)}{2 \pi }$$

  FullSimplify[(\[Pi] - (4 ArcTan[Sin[n Sqrt[2] \[Pi]]/(1 - Cos[n Sqrt[2] \[Pi]])] /. n -> 2^(n - 1)) + (2 ArcTan[Sin[n Sqrt[2] \[Pi]]/(
  1 - Cos[n Sqrt[2] \[Pi]])] /. n -> 2^n))/(2 \[Pi])] // Expand

$$B_n = \frac{1}{2}-\frac{2 \tan ^{-1}\left(\cot \left(2^{-\frac{3}{2}+n} \pi \right)\right)}{\pi }+\frac{\tan ^{-1}\left(\cot \left(2^{-\frac{1}{2}+n} \pi \right)\right)}{\pi }$$

POSTED BY: Mariusz Iwaniuk
POSTED BY: Sam Carrettie
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard
Be respectful. Review our Community Guidelines to understand your role and responsibilities. Community Terms of Use