Let's define a sequence of integer square roots. First, a(1) = 1. Then, a(n) is the smallest positive integer not seen before such that
sqrt(a(n) + sqrt(a(n-1) + sqrt(... + sqrt(a(1)))))
is an integer. Some examples:
a(2) is 3 because it's the smallest integer such that sqrt(a(2) + sqrt(a(1))) = sqrt(a(2) + 1)
is integer, and 3 hasn't occured in the sequence before.
a(3) is 2 because it's the smallest integer such that sqrt(a(3) + sqrt(a(2) + sqrt(a(1)))) = sqrt(a(3) + 2)
is integer, and 2 hasn't occured in the sequence before.
a(4) is 7 because sqrt(a(4) + 2)
is integer. We couldn't have a(4) = 2 because 2 already occured in our sequence.
Write a program or function that given a parameter n returns a sequence of numbers a(1) to a(n).
The sequence starts 1,3,2,7,6,13,5, ....
Source of this sequence is from this Math.SE question.
A plot of the first 1000 elements in the sequence: