How to calculate all points for which this program won't halt and what $e$ have in common with this?

137 Views Asked by At

Main

I was playing python and created very intresting figure: enter image description here

It was done by: $$ x_{n+1} = 1 \bmod x_{n} $$ $$ x_0 \in [0,1) $$ And the code is:

  while x != 0:
    x = 1 % x

I wonder if it is possible to calculate all values for which this program won't halt.
I suspect that those special values will be irrational numbers, but how to properly calculate all of them?

With using numerical approach I have approximated one of the branch: enter image description here Which is equal to: $$ 0.63212055882855767840447622983853913255418886896823... \approx \frac{e-1}{e} $$

What is more, the slope of these straight lines (colored on red) is:
$$ (-1)^{n} n! $$

What about the rest of the branches?
Will all of them be a combination of $e$?


Edit 1

The plot is done by:

import matplotlib.pyplot as plt
import numpy as np

acc = 100_000
def func(xmin=0, xmax=1):
  px = []
  py = []
  for x in np.linspace(xmin, xmax, acc):
    x0 = x
#    px.append(x0) # <--
#    py.append(x)  # <--
    while x != 0:
      x = 1 % x
      px.append(x0)
      py.append(x)
  return px, py

px, py = func()
fig, ax = plt.subplots()
l, = plt.plot(px, py, '.k', ms=1)

plt.grid(True)
plt.show()
1

There are 1 best solutions below

2
On

As pointed out in the comments, we cannot answer questions about plots without you providing the code that generated those plots. However, it is possible to answer your first question: This program halts if and only if the initial value is rational.


Define a sequence $\{x_n\}_{n\ge 0}$ recursively, via $x_{n+1}=1-\lfloor1/x_n\rfloor x_n$. You ask for what initial values $x_0\in [0,1)$, there does not exist an $N\ge 1$ such that $x_N=0$. This formulation is not watertight, since if such an $N$ exists, $x_{N+1}$ is not well-defined, but you get the point.

First off, $x_{n+1}$ is rational if and only if $x_n$ is rational. Therefore, if $x_0$ is rational, all the $x_n$ are rational and if $x_0$ is irrational, all the $x_n$ are irrational. Therefore, we can only get $x_N=0$ for some $N\ge 1$ if $x_0$ is rational. This is essentially lulu's comment. See if you can prove this yourself using induction.


Let $n\ge 1$ and suppose $x_n=a/b$ for some integers $a,b$ with $a,b\neq 0$ and $b>0$. I claim that $x_{n+1}=r/b$ for some integer $a>r\ge 0$.

Proof: Write $b=aq+r$, where $q\ge 1$ and $a>r\ge 0$ are integers, then $$ x_{n+1} = 1-\lfloor 1/x_n\rfloor x_n = 1-\lfloor \frac{aq+r}{a}\rfloor\cdot \frac ab=1-q\cdot\frac{a}{b}=\frac{b-qa}{b}=\frac rb. $$ We conclude that if $x_n$ is rational, there exits an $N\ge 1$ such that $x_N=0$.