While29. A real number \(\varepsilon\) (> 0). The sequence of real numbers \(A_K\) is defined as follows:
\(A_1 = 1, \quad A_2 = 2, \quad A_K = (A_{K-2} + 2 \cdot A_{K-1})/3, \quad K = 3, 4, ...\) .
Find the first of the numbers K for which the condition \(|A_K  - A_{K-1}| < \varepsilon\) is fulfilled, and print this number, as well as the numbers \(A_{K-1}\) and \(A_K\).

Solution in Python 3:

import random

N = random.randrange(1,15)
eps = 1/10**N
print("N = ",N)
print("eps = ",eps)

A1 = 1
A2 = 2
A3 = (A1 + 2*A2)/3
print(1,":",A1)
print(2,":",A2)
print(3,":",A3)
k = 3
while abs(A3 - A2) >= eps:
A2, A3 = A3, (A2 + 2*A3)/3
k += 1
print(k,":",A3)
print()
print(k,":",A2,":",A3)