This code-golf challenge will give you an integer n
, and ask you to count the number of positive integer sequences \$S = (a_1, a_2, \dots, a_t)\$ such that
- \$a_1 + a_2 + \cdots + a_t = n\$, and
- \$\displaystyle \sqrt{a_1+\sqrt{a_2 + \cdots + \stackrel{\vdots}{\sqrt{a_t}}}} \$ is an integer.
Example
If n = 14
, then there are 8 such sequences:
- \$\sqrt{2+\sqrt{2+\sqrt{2+\sqrt{2+\sqrt{2+\sqrt{3+\sqrt{1}}}}}}} = 2\$
- \$\sqrt{2+\sqrt{2+\sqrt{2+\sqrt{2+\sqrt{2+\sqrt{4}}}}}} = 2\$
- \$\sqrt{1+\sqrt{7+\sqrt{2+\sqrt{3+\sqrt{1}}}}} = 2\$
- \$\sqrt{2+\sqrt{1+\sqrt{7+\sqrt{3+\sqrt{1}}}}} = 2\$
- \$\sqrt{2+\sqrt{2+\sqrt{1+\sqrt{8+\sqrt{1}}}}} = 2\$
- \$\sqrt{1+\sqrt{7+\sqrt{2+\sqrt{4}}}} = 2\$
- \$\sqrt{2+\sqrt{1+\sqrt{7+\sqrt{4}}}} = 2\$
- \$\sqrt{2+\sqrt{2+\sqrt{1+\sqrt{9}}}} = 2\$
(In this example, all of the nested square root expressions are equal to 2, but in general, this may not be the case.)
Pairs \$(n,(a(n))\$ for \$n \leq 25\$:
(1,1),(2,0),(3,0),(4,2),(5,0),(6,2),(7,0),(8,2),(9,2),(10,4),(11,2),(12,6),(13,2),(14,8),(15,4),(16,14),(17,6),(18,20),(19,8),(20,28),(21,14),(22,44),(23,20),(24,66),(25,30)
Your code must be robust against floating-point errors, that is it must work for arbitrarily large inputs, in principle.
Since this is a code-golf challenge, the shortest code wins.
(This is now on the On-Line Encyclopedia of Integer Sequences as A338271. Sequence A338268 has been added too, based on Bubbler's \$f\$ function.)