interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / hofstadter-q-sequence / oracle.py

12 lines · 598 bytes Raw Blame History
1# Independent reference for "Hofstadter Q sequence" (Python 3, standard library only).
2# expected.txt is this program's stdout; omc never produced it.
3q = {1: 1, 2: 1}
4def 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]
9Q(100000)
10print("first 10 terms: " + " ".join(str(Q(n)) for n in range(1, 11)))
11print(f"the 1000th term: {Q(1000)}")
12print(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])}")