NestWhile
Usage
• NestWhile[f, expr, test]以expr开始,然后重复使用f 直至把test应用到该结果不再得到True为止.
• NestWhile[f, expr, test, m] 提供最近的m的结果作为每一步test的参数.
• NestWhile[f, expr, test, All]提供迄今为止的All结果作为每一步test的参数.
• NestWhile[f, expr, test, m, max] 至多把 f 使用 max 次.
• NestWhile[f, expr, test, m, max, n]把 f 额外使用 n 次.
• NestWhile[f, expr, test, m, max, -n] 返回在 f 已被使用少于 n 次时所找到的结果.
Notes
• NestWhile[f, expr, test] 返回被应用到test上不返回True的第一个表达式 f[f[... f[expr]... ]]. • 如果 test[expr] 不为 True, NestWhile[f, expr, test] 返回 expr. • NestWhile[f, expr, test, m] 在每一步对 test[ , , ... , ]进行计算. 它不把结果  放在一个列表中. •  以它们被生成的顺序给出, 最近的跟在最后. • 直到至少有m个结果已被生成时,NestWhile[f, expr, test, m]才开始应用test. • 直到至少有 mmin个结果已被生成时, NestWhile[f, expr, test, mmin, m ]才开始应用 test.然后它在每一步为 test提供不超过 m的尽可能多的最近结果作为参数. • NestWhile[f, expr, test, m] 等价于 NestWhile[f, expr, test, m, m ]. • NestWhile[f, expr, UnsameQ, 2] 等价于 FixedPoint[f, expr]. • NestWhile[f, expr, test, All] 等价于 NestWhile[f, expr, test, 1, Infinity ]. • NestWhile[f, expr, UnsameQ, All] 不断应用 f 直至相同结果首次出现不止一次. • NestWhile[f, expr, test, m, max, n] 在 test 失败 或 max 个应用程序已被执行之后把 f 额外使用 n 次 . • NestWhile[f, expr, test, m, max, -n] 等价于 Part[NestWhileList[f, expr, test, m, max], -n-1]. • 如果可能的话,NestWhile[f, expr, test, m, Infinity, -1] 返回序列 expr, f[expr], f[f[expr]], ... 中的最后一个表达式,对这个表达式 test 得到 True.
Further Examples
This gives the first prime after .
In[1]:=
|
Out[1]=
|
is the th integer counting from .
In[2]:=
|
Out[2]=
|
Here is a function that takes a single step in the Newton approximation to .
In[3]:=
|
Here are two ways to get a good approximation to .
In[4]:=
|
Out[4]=
|
In[5]:=
|
Out[5]=
|
Here are two more ways to get the same result.
In[6]:=
|
Out[6]=
|
In[7]:=
|
Out[7]=
|
The sum of the reciprocals of all the prime numbers diverges. T[x] gives the number of terms needed for the sum to exceed x. (Since the sum grows very slowly this method takes a long time even on a fast computer.)
In[8]:=
|
In[9]:=
|
Out[9]=
|
In[10]:=
|
|