ŻJ²p⁸§Ṭ¬TŻḣðƬṪ
A monadic Link accepting a positive integer \$N\$ that yields a list of the first \$N\$ terms.
Try it online!
How?
Iterates towards the greedy solution. It would be easier to follow as 0ð...
but we can start the Ƭ
-loop with \$N\$ rather than \$0\$ saving those two bytes.
ŻJ²p⁸§Ṭ¬TŻḣðƬṪ - Link: N
Ƭ - start with a left argument of N, apply repeatedly until no change, collecting
the left arguments in a list:
ð - dyadic chain - f(L=current left argument, N)
Ż - when L is an integer [0..L] (first pass only)
when L is a list [0]+L (all other passes)
J - range of length -> [1..n+1] (all passes due to ḣ, below)
² - square these -> [1,4,9,...,(n+1)²]
⁸ - L (on the first pass L is N and this is implicitly cast to [1..N])
p - (squares) Cartesian product (L)
§ - sums -> numbers which are any of L plus any of these squares
Ṭ - untruth -> a binary list with ones at those indices
¬ - logical NOT
T - truth -> a list of the indices of the truthy entries
i.e. a bunch of numbers that aren't in the result of the sums, §
Ż - prepend a zero
ḣ - head to length (N)
Ṫ - tail
As an example of the Ƭ
-loop consider \$N=3\$:
>>> L = 3
ŻJ² -> [1, 4, 9, 16]
ŻJ²p⁸ -> [[1, 1], [1, 2], [1, 3], [4, 1], [4, 2], [4, 3], [9, 1], [9, 2], [9, 3], [16, 1], [16, 2], [16, 3]]
ŻJ²p⁸§ -> [2, 3, 4, 5, 6, 7, 10, 11, 12, 17, 18, 19]
ŻJ²p⁸§Ṭ¬TŻ -> [0, 1, 8, 9, 13, 14, 15, 16]
ŻJ²p⁸§Ṭ¬TŻḣ -> [0, 1, 8]
>>> L = [0, 1, 8]
ŻJ² -> [1, 4, 9, 16]
ŻJ²p⁸ -> [[1, 0], [1, 1], [1, 8], [4, 0], [4, 1], [4, 8], [9, 0], [9, 1], [9, 8], [16, 0], [16, 1], [16, 8]]
ŻJ²p⁸§ -> [1, 2, 9, 4, 5, 12, 9, 10, 17, 16, 17, 24]
ŻJ²p⁸§Ṭ¬TŻ -> [0, 3, 6, 7, 8, 11, 13, 14, 15, 18, 19, 20, 21, 22, 23]
ŻJ²p⁸§Ṭ¬TŻḣ -> [0, 3, 6]
>>> L = [0, 3, 6]
ŻJ² -> [1, 4, 9, 16]
ŻJ²p⁸ -> [[1, 0], [1, 3], [1, 6], [4, 0], [4, 3], [4, 6], [9, 0], [9, 3], [9, 6], [16, 0], [16, 3], [16, 6]]
ŻJ²p⁸§ -> [1, 4, 7, 4, 7, 10, 9, 12, 15, 16, 19, 22]
ŻJ²p⁸§Ṭ¬TŻ -> [0, 2, 3, 5, 6, 8, 11, 13, 14, 17, 18, 20, 21]
ŻJ²p⁸§Ṭ¬TŻḣ -> [0, 2, 3]
>>> L = [0, 2, 3]
ŻJ² -> [1, 4, 9, 16]
ŻJ²p⁸ -> [[1, 0], [1, 2], [1, 3], [4, 0], [4, 2], [4, 3], [9, 0], [9, 2], [9, 3], [16, 0], [16, 2], [16, 3]]
ŻJ²p⁸§ -> [1, 3, 4, 4, 6, 7, 9, 11, 12, 16, 18, 19]
ŻJ²p⁸§Ṭ¬TŻ -> [0, 2, 5, 8, 10, 13, 14, 15, 17]
ŻJ²p⁸§Ṭ¬TŻḣ -> [0, 2, 5]
>>> L = [0, 2, 5]
ŻJ² -> [1, 4, 9, 16]
ŻJ²p⁸ -> [[1, 0], [1, 2], [1, 5], [4, 0], [4, 2], [4, 5], [9, 0], [9, 2], [9, 5], [16, 0], [16, 2], [16, 5]]
ŻJ²p⁸§ -> [1, 3, 6, 4, 6, 9, 9, 11, 14, 16, 18, 21]
ŻJ²p⁸§Ṭ¬TŻ -> [0, 2, 5, 7, 8, 10, 12, 13, 15, 17, 19, 20]
ŻJ²p⁸§Ṭ¬TŻḣ -> [0, 2, 5]
>>> [0, 2, 5]
>>> seen previously, so stop the Ƭ-loop and yield:
[3, [0, 1, 8], [0, 3, 6], [0, 2, 3], [0, 2, 5]]
Ṫ -> [0, 2, 5]