# Python program for A374141, A374142, and A374143
# Michael S. Branicky, Jun 29 2024

# A374141 a(n) is the smallest number which can be represented as the sum of two distinct nonzero hexagonal numbers in exactly n ways, or -1 if no such number exists.
A374141_data = [7, 384, 4995, 51106, 204805, 483031, 2443431, 4674256, 10476781, 17272531, 25600656, 60765331, 90406956, 206602126, 332808531, 481676406, 303826656, 435211156, 789949306, 1406495106, 2260173906, 2704798281, 3220562556, 4435869181, 5165053156, 5309576106, 9818788281]

# A374142 a(n) is the smallest number which can be represented as the sum of two distinct nonzero heptagonal numbers in exactly n ways, or -1 if no such number exists.
A374142_data = [8, 617, 8726, 255575, 1339801, 2419165, 9402323, 25764500, 35486953, 144568133, 385495261, 735503569, 638279039, 1183071664, 1571634527, 4449717748, 3584182298, 3871587494, 5693954599, 27084640649, 24205505111, 32489035067, 31973745058, 38935021406, 47570693867, 44749048300, 53075499329]

# A374143 a(n) is the smallest number which can be represented as the sum of two distinct nonzero octagonal numbers in exactly n ways, or -1 if no such number exists.
A374143_data = [9, 1053, 12641, 68141, 365641, 953181, 2830641, 6232341, 13969041, 23211261, 104733741, 84994021, 175873641, 159851141, 538547641, 602713041, 810204416, 1019740041, 1053265741, 1972957241, 3339356041, 5914492241, 6886737541, 6388758241, 8902368041, 7858982841, 4942246941, 18439299341, 26639916441]

# (Python)
import heapq
from itertools import islice

def A000384(i): # hexagonal numbers
    return i*(2*i-1)

def A000566(i): # heptagonal numbers
    return i*(5*i-3)//2

def A000567(i): # octagonal numbers
    return i*(3*i-2)

# generates the smallest number which can be represented as
# the sum of two distinct "f(i)" numbers in exactly n ways.
def agen(f=A000384):
    f1, f2 = f(1), f(2)
    p = v = f1+f2
    h, nextcount = [(v, 1, 2)], 3
    oldv = ways = highways = 0
    waysdict = dict()
    n = 1
    while True:
        (v, s, l) = heapq.heappop(h)
        if v == oldv: ways += 1
        else:
            if ways not in waysdict:
                waysdict[ways] = oldv
                while n in waysdict:
                    yield waysdict[n]
                    n += 1
            ways = 1
        if v >= p:
            p = f1 + f(nextcount)
            heapq.heappush(h, (p, 1, nextcount))
            nextcount += 1
        oldv = v
        s += 1; l += 1
        heapq.heappush(h, (f(s)+f(l), s, l))

# A374141
print(list(islice(agen(f=A000384), 10)))
print(A374141_data)
print()

# A374142
print(list(islice(agen(f=A000566), 7)))
print(A374142_data)
print()

# A374143
print(list(islice(agen(f=A000567), 10)))
print(A374143_data)
print()