from queue import PriorityQueue from functools import reduce import operator # n-th row of A081957 def row(n): """ row(n) is computed by enumerating all pairs (p, v) where v is a list of n distinct integers >1 and p = prod(v). The pairs are enumerated in increasing order by p by using a priority queue and a search tree with a monotonicity property. Note that certain p may appear multiple times in the queue, but the integers in row(n) must be distinct. """ v = list(range(2, n+2)) res = [] visited = set() pq = PriorityQueue() pq.put((reduce(operator.mul, v, 1), v)) while len(res) < n: prod, v = pq.get() if tuple(v) not in visited: visited.add(tuple(v)) if len(res) == 0 or res[-1] != prod: res.append(prod) for i in range(n): if i == n-1 or v[i] + 1 < v[i+1]: prod //= v[i] v[i] += 1 prod *= v[i] pq.put((prod, list(v))) prod //= v[i] v[i] -= 1 prod *= v[i] return res a_list = sum([row(i) for i in range(1, 150)], []) for i in range(10000): print(i+1, a_list[i])