GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
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
Examples
Basic Examples
Scope
Generalizations & Extensions
Applications
Properties & Relations
Neat Examples
See Also
Tech Notes
Related Guides
Related Links
History
Cite this Page
NestWhileListNestWhileList[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
Examplesopen all close allBasic Examples (2)Keep dividing by 2 until the result is no longer an even number: NestWhileList[# / 2&, 123456, EvenQ]Iterate taking logarithms until the result is no longer positive: NestWhileList[Log, 100, # > 0&]Scope (4)Start comparisons after 4 iterations, and compare using the 4 last values: NestWhileList[Floor[# / 2]&, 10, (Print[{##}];UnsameQ[##])&, 4]Start comparisons after 4 iterations, and compare using the 6 last values: NestWhileList[Floor[# / 2]&, 10, (Print[{##}];UnsameQ[##])&, {4, 6}]Always compare all values generated: NestWhileList[Floor[# / 2]&, 10, (Print[{##}];UnsameQ[##])&, All]Stop after at most 4 steps, even if the condition is still True: NestWhileList[Floor[# / 2]&, 20, UnsameQ, 2, 4]Generalizations & Extensions (1)Continue until the result is no longer greater than 1: NestWhileList[Floor[# / 2]&, 20, # > 1&, 1, ∞]Perform one more step after the condition is no longer True: NestWhileList[Floor[# / 2]&, 20, # > 1&, 1, ∞, 1]Drop the last value generated (for which the test was no longer True): NestWhileList[Floor[# / 2]&, 20, # > 1&, 1, ∞, -1]Applications (5)Find successive integers until a prime is reached: NestWhileList[# + 1&, 899, !PrimeQ[#]&]Find the multiplicative order of 2 modulo 19: NestWhileList[Mod[2#, 19]&, 2, # != 1&]Length[%]Use MultiplicativeOrder to compute directly: MultiplicativeOrder[2, 19]Find the orbit of NestWhileList[Mod[5 #, 7]&, 4, Unequal, All]Keep applying iterations in the NestWhileList[If[EvenQ[#], # / 2, (3# + 1) / 2]&, 400, Unequal, All]Exclude the first repeating element from the output: NestWhileList[If[EvenQ[#], # / 2, (3# + 1) / 2]&, 400, Unequal, All, ∞, -1]Apply Newton iterations for NestWhileList[(# + 2 / # ) / 2&, 1, Abs[#1 - #2] > 0.001&, 2]Properties & Relations (3)These two forms are equivalent: NestWhileList[Floor[# / 2]&, 10, UnsameQ, {4, 4}]NestWhileList[Floor[# / 2]&, 10, UnsameQ, 4]NestWhileList returns all intermediate values of NestWhile: NestWhileList[# / 2&, 123456, EvenQ]Reap[NestWhile[(Sow[#] / 2)&, 123456, EvenQ]]FixedPointList always compares the last two values; these two forms are equivalent: NestWhileList[(# + 3 / #) / 2&, 1.0, UnsameQ, 2]FixedPointList[(# + 3 / #) / 2&, 1.0]Neat Examples (2)Find the digits of a number: With[{n = 123456, k = 10}, Reverse[Mod[NestWhileList[Quotient[#, k]&, n, # ≥ k&], k]]]Distance of two vertices in a graph: Distance[g_, {i_, j_}] := Length[NestWhileList[Union[Flatten[# /. g]]&, {i}, !MemberQ[#, j]&]] - 1dodec = {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}};Distance[dodec, {1, 11}]A plot of the graph: GraphPlot[Flatten[Map[Thread, dodec]], PlotTheme -> "ClassicDiagram"]See AlsoTech NotesRelated GuidesRelated LinksHistoryIntroduced in 1999 (4.0)
Wolfram Research (1999), NestWhileList, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhileList.html.
TextWolfram Research (1999), NestWhileList, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhileList.html. CMSWolfram Language. 1999. "NestWhileList." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NestWhileList.html. APAWolfram 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]} |
| 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. |