WOLFRAM

NestWhileList[f,expr,test]

generates a list of the results of applying f repeatedly, starting with expr, and continuing until applying test to the result no longer yields True.

NestWhileList[f,expr,test,m]

supplies the most recent m results as arguments for test at each step.

NestWhileList[f,expr,test,All]

supplies all results so far as arguments for test at each step.

NestWhileList[f,expr,test,m,max]

applies f at most max times.

Details

  • The last element of the list returned by NestWhileList[f,expr,test] is always an expression to which applying test does not yield True.
  • NestWhileList[f,expr,test,m] at each step evaluates test[res1,res2,,resm]. It does not put the results resi in a list.
  • The resi are given in the order they are generated, with the most recent coming last.
  • NestWhileList[f,expr,test,m] does not start applying test until at least m results have been generated. »
  • NestWhileList[f,expr,test,{mmin,m}] does not start applying test until at least mmin results have been generated. At each step it then supplies as arguments to test as many recent results as possible, up to a maximum of m. »
  • NestWhileList[f,expr,test,m] is equivalent to NestWhileList[f,expr,test,{m,m}]. »
  • NestWhileList[f,expr,UnsameQ,2] is equivalent to FixedPointList[f,expr]. »
  • NestWhileList[f,expr,test,All] is equivalent to NestWhileList[f,expr,test,{1,Infinity}]. »
  • NestWhileList[f,expr,UnsameQ,All] goes on applying f until the same result first appears more than once.
  • NestWhileList[f,expr,test,m,max,n] applies f an extra n times, appending the results to the list generated. »
  • NestWhileList[f,expr,test,m,max,-n] drops the last n elements from the list generated. »

Examples

open allclose all

Basic Examples  (2)Summary of the most common use cases

Keep dividing by 2 until the result is no longer an even number:

Out[1]=1

Iterate taking logarithms until the result is no longer positive:

Out[1]=1

Scope  (4)Survey of the scope of standard use cases

Start comparisons after 4 iterations, and compare using the 4 last values:

Out[1]=1

Start comparisons after 4 iterations, and compare using the 6 last values:

Out[1]=1

Always compare all values generated:

Out[1]=1

Stop after at most 4 steps, even if the condition is still True:

Out[1]=1

Generalizations & Extensions  (1)Generalized and extended use cases

Continue until the result is no longer greater than 1:

Out[1]=1

Perform one more step after the condition is no longer True:

Out[2]=2

Drop the last value generated (for which the test was no longer True):

Out[3]=3

Applications  (5)Sample problems that can be solved with this function

Find successive integers until a prime is reached:

Out[1]=1

Find the multiplicative order of 2 modulo 19:

Out[1]=1
Out[2]=2

Use MultiplicativeOrder to compute directly:

Out[3]=3

Find the orbit of under the mapping :

Out[1]=1

Keep applying iterations in the problem until the results repeat:

Out[1]=1

Exclude the first repeating element from the output:

Out[2]=2

Apply Newton iterations for until successive results are within 0.001:

Out[1]=1

Properties & Relations  (3)Properties of the function, and connections to other functions

These two forms are equivalent:

Out[1]=1
Out[2]=2

NestWhileList returns all intermediate values of NestWhile:

Out[1]=1
Out[2]=2

FixedPointList always compares the last two values; these two forms are equivalent:

Out[1]=1
Out[2]=2

Neat Examples  (2)Surprising or curious use cases

Find the digits of a number:

Out[1]=1

Distance of two vertices in a graph:

Out[3]=3

A plot of the graph:

Out[4]=4
Wolfram Research (1999), NestWhileList, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhileList.html.
Wolfram Research (1999), NestWhileList, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhileList.html.

Text

Wolfram Research (1999), NestWhileList, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhileList.html.

Wolfram Research (1999), NestWhileList, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhileList.html.

CMS

Wolfram Language. 1999. "NestWhileList." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NestWhileList.html.

Wolfram Language. 1999. "NestWhileList." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NestWhileList.html.

APA

Wolfram Language. (1999). NestWhileList. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NestWhileList.html

Wolfram Language. (1999). NestWhileList. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NestWhileList.html

BibTeX

@misc{reference.wolfram_2025_nestwhilelist, author="Wolfram Research", title="{NestWhileList}", year="1999", howpublished="\url{https://reference.wolfram.com/language/ref/NestWhileList.html}", note=[Accessed: 22-May-2025 ]}

@misc{reference.wolfram_2025_nestwhilelist, author="Wolfram Research", title="{NestWhileList}", year="1999", howpublished="\url{https://reference.wolfram.com/language/ref/NestWhileList.html}", note=[Accessed: 22-May-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_nestwhilelist, organization={Wolfram Research}, title={NestWhileList}, year={1999}, url={https://reference.wolfram.com/language/ref/NestWhileList.html}, note=[Accessed: 22-May-2025 ]}

@online{reference.wolfram_2025_nestwhilelist, organization={Wolfram Research}, title={NestWhileList}, year={1999}, url={https://reference.wolfram.com/language/ref/NestWhileList.html}, note=[Accessed: 22-May-2025 ]}