Skip to content

NestWhile ​

Applies a function repeatedly while a test on the current value holds.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageNestWhile✓
NestWhile(f, x, test)Applies ff to xx repeatedly while testtest of the CURRENT value reads TrueTrue, and returns the first value where it doesn't.
NestWhile(f, x, test, m)testtest is called on the last mm values as mm separate arguments, instead of the bare current value.
NestWhile(f, x, test, m, max)Additionally caps the number of ff-applications at maxmax.
Typed signatures
TypePackageApplies
(any, any, any, integer?, integer?) -> anycombinatoricsalways

Domain: Collections

Details
  • With m>1m > 1, the test waits until mm values exist, so ff runs m−1m - 1 times before the first test.
  • Capped at 4096 applications of ff regardless of maxmax — see While.

Examples

Basic #

halve while even, stopping at the first odd value

live ↗
InNestWhile(_1⁡↦_1⁡2,96,_1⁡↦_1⁡ mod 2=0)\mathrm{NestWhile}(\operatorname{\_1}↦\frac{\operatorname{\_1}}{2}, 96, \operatorname{\_1}↦\operatorname{\_1}\bmod2=0)Out33

Divides by 22 while even, leaving the odd part: 123456=26⋅1929123456 = 2^6\cdot1929

live ↗
InNestWhile(_1⁡↦_1⁡2,123 456,IsEven)\mathrm{NestWhile}(\operatorname{\_1}↦\frac{\operatorname{\_1}}{2}, 123\,456, \mathrm{IsEven})Out1 9291\,929

Iterated logs of 100100 until the value turns negative; stays symbolic

live ↗
InNestWhile(ln⁡,100,_1⁡↦_1⁡>0)\mathrm{NestWhile}(\ln, 100, \operatorname{\_1}↦\operatorname{\_1}\gt0)Outln⁡(ln⁡(ln⁡(2ln⁡(10))))\ln(\ln(\ln(2\ln(10))))
Scope #

With a lookback of one and a cap of four steps, it stops early

live ↗
InNestWhile(_1⁡↦_1⁡2,123 456,IsEven,1,4)\mathrm{NestWhile}(\operatorname{\_1}↦\frac{\operatorname{\_1}}{2}, 123\,456, \mathrm{IsEven}, 1, 4)Out7 7167\,716
live ↗
InNestWhile(_1⁡↦⌊_1⁡2⌋,10,UnsameQ,2)\mathrm{NestWhile}(\operatorname{\_1}↦\lfloor\frac{\operatorname{\_1}}{2}\rfloor, 10, \mathrm{UnsameQ}, 2)Out55
Applications #

The first prime above 888888 is 907907

live ↗
InNestWhile(_1⁡↦_1⁡+1,888,_1⁡↦¬IsPrime(_1⁡))\mathrm{NestWhile}(\operatorname{\_1}↦\operatorname{\_1}+1, 888, \operatorname{\_1}↦¬\mathrm{IsPrime}(\operatorname{\_1}))Out907907

Smallest nn with Fn>106F_n > 10^6 is 3131

live ↗
InNestWhile(_1⁡↦_1⁡+1,1,_1⁡↦Fibonacci(_1⁡)≤106)\mathrm{NestWhile}(\operatorname{\_1}↦\operatorname{\_1}+1, 1, \operatorname{\_1}↦\mathrm{Fibonacci}(\operatorname{\_1})≤10^6)Out3131
live ↗
InNestWhile(_1⁡↦_1⁡+1,888,(_1⁡,_3⁡)↦¬IsPrime(_1⁡)∨¬IsPrime(_3⁡),3)\mathrm{NestWhile}(\operatorname{\_1}↦\operatorname{\_1}+1, 888, (\operatorname{\_1}, \operatorname{\_3})↦¬\mathrm{IsPrime}(\operatorname{\_1})∨¬\mathrm{IsPrime}(\operatorname{\_3}), 3)Out1 0211\,021
Properties #

Halves with floor until it hits 00

live ↗
InNestWhile(_1⁡↦⌊_1⁡2⌋,10,_1⁡↦_1⁡>0)\mathrm{NestWhile}(\operatorname{\_1}↦\lfloor\frac{\operatorname{\_1}}{2}\rfloor, 10, \operatorname{\_1}↦\operatorname{\_1}\gt0)Out00
live ↗
InNestWhile(_1⁡↦⌊_1⁡2⌋,10,UnsameQ,4)\mathrm{NestWhile}(\operatorname{\_1}↦\lfloor\frac{\operatorname{\_1}}{2}\rfloor, 10, \mathrm{UnsameQ}, 4)Out11
live ↗
InNestWhile(_1⁡↦11PrimePi(_1⁡)+1,2,UnsameQ,2)\mathrm{NestWhile}(\operatorname{\_1}↦11\mathrm{PrimePi}(\operatorname{\_1})+1, 2, \mathrm{UnsameQ}, 2)Out1212
About
Examples11
Checked againstWolfram Language 11 agree
CrosswalkWolfram Language

See also: NestWhileList, While, FixedPointList