GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • WOLFRAM
  • Products
  • AI AccessUse with Mathematica and Wolfram|One
  • Finance Platform
  • Wolfram|Alpha Notebook Edition
  • Application Server
  • Enterprise Private Cloud
  • Wolfram Engine
  • Wolfram Player
  • Wolfram Cloud App
  • Wolfram Player App
  • Wolfram|Alpha Website
  • Wolfram|Alpha APIs
  • Wolfram AI Ecosystem

This view

  • Original page
  • New address

GeezerVison is showing reference.wolfram.com — NestWhileList—Wolfram Documentation

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
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Generalizations & Extensions  
Applications  
Properties & Relations  
Neat Examples  
See Also
Tech Notes
Related Guides
Related Links
History
Cite this Page

NestWhileList

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 all close all

Basic Examples  (2)

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

Wolfram Language code: NestWhileList[# / 2&, 123456, EvenQ]

Iterate taking logarithms until the result is no longer positive:

Wolfram Language code: NestWhileList[Log, 100, # > 0&]

Scope  (4)

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

Wolfram Language code: NestWhileList[Floor[# / 2]&, 10, (Print[{##}];UnsameQ[##])&, 4]

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

Wolfram Language code: NestWhileList[Floor[# / 2]&, 10, (Print[{##}];UnsameQ[##])&, {4, 6}]

Always compare all values generated:

Wolfram Language code: NestWhileList[Floor[# / 2]&, 10, (Print[{##}];UnsameQ[##])&, All]

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

Wolfram Language code: NestWhileList[Floor[# / 2]&, 20, UnsameQ, 2, 4]

Generalizations & Extensions  (1)

Continue until the result is no longer greater than 1:

Wolfram Language code: NestWhileList[Floor[# / 2]&, 20, # > 1&, 1, ∞]

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

Wolfram Language code: NestWhileList[Floor[# / 2]&, 20, # > 1&, 1, ∞, 1]

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

Wolfram Language code: NestWhileList[Floor[# / 2]&, 20, # > 1&, 1, ∞, -1]

Applications  (5)

Find successive integers until a prime is reached:

Wolfram Language code: NestWhileList[# + 1&, 899, !PrimeQ[#]&]

Find the multiplicative order of 2 modulo 19:

Wolfram Language code: NestWhileList[Mod[2#, 19]&, 2, # != 1&]
Wolfram Language code: Length[%]

Use MultiplicativeOrder to compute directly:

Wolfram Language code: MultiplicativeOrder[2, 19]

Find the orbit of under the mapping :

Wolfram Language code: NestWhileList[Mod[5 #, 7]&, 4, Unequal, All]

Keep applying iterations in the problem until the results repeat:

Wolfram Language code: NestWhileList[If[EvenQ[#], # / 2, (3# + 1) / 2]&, 400, Unequal, All]

Exclude the first repeating element from the output:

Wolfram Language code: NestWhileList[If[EvenQ[#], # / 2, (3# + 1) / 2]&, 400, Unequal, All, ∞, -1]

Apply Newton iterations for until successive results are within 0.001:

Wolfram Language code: NestWhileList[(# + 2 / # ) / 2&, 1, Abs[#1 - #2] > 0.001&, 2]

Properties & Relations  (3)

These two forms are equivalent:

Wolfram Language code: NestWhileList[Floor[# / 2]&, 10, UnsameQ, {4, 4}]
Wolfram Language code: NestWhileList[Floor[# / 2]&, 10, UnsameQ, 4]

NestWhileList returns all intermediate values of NestWhile:

Wolfram Language code: NestWhileList[# / 2&, 123456, EvenQ]
Wolfram Language code: Reap[NestWhile[(Sow[#] / 2)&, 123456, EvenQ]]

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

Wolfram Language code: NestWhileList[(# + 3 / #) / 2&, 1.0, UnsameQ, 2]
Wolfram Language code: FixedPointList[(# + 3 / #) / 2&, 1.0]

Neat Examples  (2)

Find the digits of a number:

Wolfram Language code: With[{n = 123456, k = 10}, Reverse[Mod[NestWhileList[Quotient[#, k]&, n, # ≥ k&], k]]]

Distance of two vertices in a graph:

Wolfram Language code: Distance[g_, {i_, j_}] := Length[NestWhileList[Union[Flatten[# /. g]]&, {i}, !MemberQ[#, j]&]] - 1
Wolfram Language code: dodec = {1 -> {2, 17, 20}, 2 -> {1, 3, 15}, 3 -> {13, 2, 4}, 4 -> {20, 5, 3}, 5 -> {12, 6, 4}, 6 -> {5, 7, 19}, 7 -> {6, 8, 11}, 8 -> {18, 7, 9}, 9 -> {8, 10, 16}, 10 -> {9, 11, 14}, 11 -> {10, 12, 7}, 12 -> {11, 13, 5}, 13 -> {12, 14, 3}, 14 -> {13, 15, 10}, 15 -> {14, 16, 2}, 16 -> {15, 17, 9}, 17 -> {16, 18, 1}, 18 -> {17, 19, 8}, 19 -> {18, 20, 6}, 20 -> {19, 1, 4}};
Wolfram Language code: Distance[dodec, {1, 11}]

A plot of the graph:

Wolfram Language code: GraphPlot[Flatten[Map[Thread, dodec]], PlotTheme -> "ClassicDiagram"]

See Also

NestWhile  FixedPointList  NestList  Nest  While

Tech Notes

    ▪
  • Applying Functions Repeatedly

Related Guides

    ▪
  • Functional Iteration
  • ▪
  • Computational Systems
  • ▪
  • Constructing Lists
  • ▪
  • Iterated Maps & Fractals
  • ▪
  • Functional Programming

Related Links

  • NKS|Online  (A New Kind of Science)

History

Introduced in 1999 (4.0)

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.

CMS

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

BibTeX

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

BibLaTeX

@online{reference.wolfram_2026_nestwhilelist, organization={Wolfram Research}, title={NestWhileList}, year={1999}, url={https://reference.wolfram.com/language/ref/NestWhileList.html}, note=[Accessed: 12-November-2026]}

Top
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.