omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / hofstadter-q-sequence / oracle.py
| 1 | # Independent reference for "Hofstadter Q sequence" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | q = {1: 1, 2: 1} |
| 4 | def Q(n): |
| 5 | # fill Q(len(q)+1) .. Q(n) in order; each term uses only earlier ones |
| 6 | for m in range(len(q) + 1, n + 1): |
| 7 | q[m] = q[m - q[m - 1]] + q[m - q[m - 2]] |
| 8 | return q[n] |
| 9 | Q(100000) |
| 10 | print("first 10 terms: " + " ".join(str(Q(n)) for n in range(1, 11))) |
| 11 | print(f"the 1000th term: {Q(1000)}") |
| 12 | print(f"terms up to the 100000th that are less than the term before: {sum(1 for n in range(2, 100001) if q[n] < q[n - 1])}") |