1
$\begingroup$

Is the following code efficient? Has it any advantages?

s[0] = Sqrt[2]; 
Table[s[t + 1] = s[t]^2, {t, 0, 4}]

{2, 4, 16, 256, 65536}

Should be efficient in the following case?

s[0] = Sqrt[2];
Table[s[t + 1] = (1 + t)*s[t]^2, {t, 0, 4}]

{2, 8, 192, 147456, 108716359680}

How could a "stopping rule" alla NestWhileList be implemented?

Thanks!

$\endgroup$
3
  • $\begingroup$ Hi Dennis! The only accepted language here is English. If you need help translating your questions or answers, other users can help you. Just ask for help in chat!. Suerte, causa! $\endgroup$ Oct 24 '14 at 2:00
  • 2
    $\begingroup$ This question appears to be off-topic because it is not formulated in English. $\endgroup$
    – m_goldberg
    Oct 24 '14 at 3:45
  • $\begingroup$ @m_goldberg Voted to close too. Then I thought again :) $\endgroup$ Oct 24 '14 at 4:31
2
$\begingroup$

I don't think you need iterations to accomplish what you are looking for.

For example:

s[0]=Sqrt[2];
s[t_] := s[t] = s[t - 1]^2  (* Use memoization *)
Map[s, Range[1,5] ]
(* {2, 4, 16, 256, 65536} *)

and in the second case:

ss[0] = Sqrt[2];
ss[t_] := ss[t] = t ss[t - 1]^2
Map[ss, Range[1,5] ]
(* {2, 8, 192, 147456, 108716359680} *)
$\endgroup$
1
$\begingroup$

If efficiency is important and you are going to generate more and more elements of your series then you might consider using RSolve to find an expression for all terms.

RSolve[{s[t + 1] == s[t]^2, s[0] == Sqrt[2]}, s[t], t]

and

RSolve[{s[t + 1] == (1 + t)*s[t]^2, s[0] == Sqrt[2]}, s[t], t]
$\endgroup$
0
$\begingroup$

Just to illustrate some alternatives:

f[n_] := Nest[#^2 &, 2, n]
fl[n_] := NestList[#^2 &, 2, n]

Then

Table[f[j], {j, 0, 4}]
f /@ Range[0, 4]
fl[4]

all yield: {2, 4, 16, 256, 65536}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.