omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / hofstadter-figure-figure-sequences / oracle.py
| 1 | # Independent reference for "Hofstadter Figure-Figure sequences" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | import itertools |
| 4 | |
| 5 | def figure_figure(): |
| 6 | # R(n) = R(n-1) + S(n-1); S runs through the positive integers that R skips |
| 7 | r, rs, s = 1, {1}, 2 |
| 8 | yield r, s |
| 9 | while True: |
| 10 | r += s |
| 11 | rs.add(r) |
| 12 | s = next(c for c in itertools.count(s + 1) if c not in rs) |
| 13 | yield r, s |
| 14 | |
| 15 | R = [r for r, _ in itertools.islice(figure_figure(), 40)] |
| 16 | S = [s for _, s in itertools.islice(figure_figure(), 960)] |
| 17 | print("R(1) to R(10): " + " ".join(map(str, R[:10]))) |
| 18 | ok = sorted(R + S) == list(range(1, 1001)) |
| 19 | print("first 40 R and first 960 S cover 1..1000 exactly once: " + ("yes" if ok else "no")) |