# How can I write this with Nest or Fold?

a = Sqrt[s^2 + s + 1] /. s -> 0
a = Sqrt[s^2 + s + 1] /. s -> a
a = Sqrt[s^2 + s + 1] /. s -> a + a
a = Sqrt[s^2 + s + 1] /. s -> a + a + a

f = Sqrt[#^2 + # + 1] &;
f
f[1 + %]
f[1 + f + %]
f[1 + f + f[1 + f] + %]
f[1 + f + f[1 + f] + f[1 + f + f[1 + f]] + %]


I have tried the following, but it didn't work well:

  Rest@FoldList[f[+##] &, 0, Table[1, 5]]

{
f,
f[1 + f],
f[1 + f[1 + f]],
f[1 + f[1 + f[1 + f]]],
f[1 + f[1 + f[1 + f[1 + f]]]]
}

• Do you mean NestList[f[1 + #] &, 0, 5]? – yode Dec 19 '16 at 5:36

NestList[# + Sqrt[#^2 + # + 1] &, 0, 4] // Differences // Column NestList[f[## & @@ # + #] &, f, 3]

{
f,
f[1 + f],
f[1 + f + f[1 + f]],
f[1 + f + f[1 + f] + f[1 + f + f[1 + f]]]
}


alternatively (but we have to start with f[1+f]:

NestList[Insert[#, #, {1, -1}] &, f[1 + f], 2]

{
f[1 + f],
f[1 + f + f[1 + f]],
f[1 + f + f[1 + f] + f[1 + f + f[1 + f]]]
}


This may not be ideal or the most elegant, but perhaps it will work or help.

Define (note that int can be whatever range you want):

In:= a = 0;
int = Range;


Create a list of symbols for your LHS:

In:= lhs = a[#] & /@ int

Out= {a, a, a, a}


This gives us your RHS's, primarily just mapping a expr /. rule expression across the iterators {1,2,3,4}:

In:= vals = (Sqrt[s[# - 1]^2 + s[# - 1] + 1] /.
s[# - 1] -> FoldList[#1 + a[#2] &, int - 1][[#]]) & /@ int

Out= {1, Sqrt[1 + a + a^2], Sqrt[
1 + a + a + (a + a)^2], Sqrt[
1 + a + a + a + (a + a + a)^2]}


And you can thread Set across them to get your end result:

In:= Thread@Set[Evaluate[lhs], vals]

Out= {1, Sqrt, Sqrt[2 + Sqrt + (1 + Sqrt)^2], Sqrt[
2 + Sqrt + Sqrt[
2 + Sqrt + (1 + Sqrt)^2] + (1 + Sqrt + Sqrt[
2 + Sqrt + (1 + Sqrt)^2])^2]}


To verify this...

In:= {a, a, a, a}

Out= {1, Sqrt, Sqrt[2 + Sqrt + (1 + Sqrt)^2], Sqrt[
2 + Sqrt + Sqrt[
2 + Sqrt + (1 + Sqrt)^2] + (1 + Sqrt + Sqrt[
2 + Sqrt + (1 + Sqrt)^2])^2]}


Like I said, you can let int be as high as you want to define as many values of a[n] are necessary. This isn't recursive, but it would work. Hoping someone can provide a better solution :)

• Thank you, but it's not I'm looking for. – matrix89 Dec 19 '16 at 6:04
• @mathe Edited it based on your edits :) – user6014 Dec 19 '16 at 6:38