Given an integer \$x_1\$ and some black box function \$f: ℤ → ℤ\$ find a fixed point of \$f\$ in the sequence defined by \$x_{k+1} := f(x_k)\$.
Details
- A value \$x\$ is said to be a fixed point of \$f\$ if \$x = f(x)\$.
For instance if \$f(x) = \text{round}(\frac{x}{\pi})\$ and we have a starting point \$x_1 = 10\$ then we get \$x_2 = f(x_1) = f(10) = 3\$, then \$x_3 = f(x_2) = f(3) = 1\$, then \$x_4 = f(x_3) = f(1) = 0\$, and finally \$x_5 = f(x_4) = f(0) = 0\$ which means the submission should return \$0\$.
- You can assume that the generated sequence actually contains a fixed point.
- You can use the native type for integers in place of \$ℤ\$.
- You can use any language for which there are defaults for black box functions input in the standard IO meta post. If there are no such default for your language feel free to add one in the sense of the definition of black box functions, and make sure to link your proposals in that definition. Also don't forget to vote on them.
Examples
f(x) = floor(sqrt(abs(x)))
0 -> 0, all other numbers -> 1
f(x) = c(c(c(x))) where c(x) = x/2 if x is even; 3*x+1 otherwise
all positive numbers should result in 1,2 or 4 (Collatz conjecture)
f(x) = -42
all numbers -> -42
f(x) = 2 - x
1 -> 1
~Nƭ⁻Ç$¿
, which is something like, (pseudo code)for x in [0, -1, 1, -2, 2, -3, 3, -4, 4, ...]: if (x == f(x)): break; print(x);
. That may worth another challenge. \$\endgroup\$