Skip to content

Nest ​

f applied n times to x.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageNest✓
Nest(f, x, n)ff applied to xx, nn times in a row.
Typed signatures
TypePackageApplies
(function: any, x: any, n: integer) -> anycombinatoricsalways

Domain: Collections

Details
  • ff can be a Function literal or a plain symbol; an undeclared symbol stays an unevaluated call, the way Nest(f,x,3)Nest(f, x, 3) shows below.
  • See NestList for every intermediate step, and FixedPoint for iterating until the value stops changing instead of a fixed count.

Examples

Basic #
live ↗
InNest(f,x,3)\mathrm{Nest}(f, x, 3)Outf(f(f(x)))f(f(f(x)))

Repeated squaring: ((22)2)2((2^2)^2)^2

live ↗
InNest(_1⁡↦_1⁡2,2,3)\mathrm{Nest}(\operatorname{\_1}↦\operatorname{\_1}^2, 2, 3)Out256256

Four nested applications of ff

live ↗
InNest(f,x,4)\mathrm{Nest}(f, x, 4)Outf(f(f(f(x))))f(f(f(f(x))))

Three steps of n↦(n+1)2n \mapsto (n+1)^2 from 11: 1,4,25,6761, 4, 25, 676

live ↗
InNest(_1⁡↦(_1⁡+1)2,1,3)\mathrm{Nest}(\operatorname{\_1}↦(\operatorname{\_1}+1)^2, 1, 3)Out676676
Scope #

Zero applications leave x alone

live ↗
InNest(f,x,0)\mathrm{Nest}(f, x, 0)Outxx
live ↗
InNest(_1⁡↦2_1⁡,1,10)\mathrm{Nest}(\operatorname{\_1}↦2\operatorname{\_1}, 1, 10)Out1 0241\,024

Four square roots of 100100, left in radical form

live ↗
InNest(Sqrt,100,4)\mathrm{Nest}(\mathrm{Sqrt}, 100, 4)Out10\sqrt{\sqrt{\sqrt{10}}}

With a symbolic start, the full nesting is retained

live ↗
InNest(_1⁡↦(_1⁡+1)2,x,5)\mathrm{Nest}(\operatorname{\_1}↦(\operatorname{\_1}+1)^2, x, 5)Out(((((x+1)2+1)2+1)2+1)2+1)2(((((x+1)^2+1)^2+1)^2+1)^2+1)^2
live ↗
InCatch(Nest(_1⁡↦if _1⁡>106 then Throw(_1⁡) else _1⁡2,2,6))\mathrm{Catch}(\mathrm{Nest}(\operatorname{\_1}↦\text{if }\operatorname{\_1}\gt10^6\text{ then }\mathrm{Throw}(\operatorname{\_1})\text{ else }\operatorname{\_1}^2, 2, 6))Out4 294 967 2964\,294\,967\,296
Applications #

Three Newton steps toward 2\sqrt{2}

live ↗
InNest(_1⁡↦_1⁡+2_1⁡2,1,3)\mathrm{Nest}(\operatorname{\_1}↦\frac{\operatorname{\_1}+\frac{2}{\operatorname{\_1}}}{2}, 1, 3)Out577408\frac{577}{408}

Builds a continued-fraction-like tower

live ↗
InNest(_1⁡↦1_1⁡+1,x,5)\mathrm{Nest}(\operatorname{\_1}↦\frac{1}{\operatorname{\_1}+1}, x, 5)Out11111/(x+1)+1+1+1+1\frac{1}{\frac{1}{\frac{1}{\frac{1}{1/(x+1)+1}+1}+1}+1}

A power tower of xx

live ↗
InNest(_1⁡↦x_1⁡,x,6)\mathrm{Nest}(\operatorname{\_1}↦x^{\operatorname{\_1}}, x, 6)Outxxxxxxxx^{x^{x^{x^{x^{x^{x}}}}}}

Newton's method for 2\sqrt2, converging quadratically to 1.41421…1.41421\ldots

live ↗
InNest(_1⁡↦_1⁡+2_1⁡2,1,5)\mathrm{Nest}(\operatorname{\_1}↦\frac{\operatorname{\_1}+\frac{2}{\operatorname{\_1}}}{2}, 1, 5)Out886 731 088 897627 013 566 048\frac{886\,731\,088\,897}{627\,013\,566\,048}
live ↗
InNest(_1⁡↦Dot([[1,1],[1,0]],_1⁡),[0,1],10)\mathrm{Nest}(\operatorname{\_1}↦\mathrm{Dot}(\bigl\lbrack\bigl\lbrack1, 1\bigr\rbrack, \bigl\lbrack1, 0\bigr\rbrack\bigr\rbrack, \operatorname{\_1}), \bigl\lbrack0, 1\bigr\rbrack, 10)Out[55,34]\bigl\lbrack55, 34\bigr\rbrack
Properties #

Halving 2020 with floor six times drains it to 00

live ↗
InNest(_1⁡↦⌊_1⁡2⌋,20,6)\mathrm{Nest}(\operatorname{\_1}↦\lfloor\frac{\operatorname{\_1}}{2}\rfloor, 20, 6)Out00
Neat examples #

Wrapping in a pair three times gives a complete binary tree of xxs

live ↗
InNest(_1⁡↦[_1⁡,_1⁡],x,3)\mathrm{Nest}(\operatorname{\_1}↦\bigl\lbrack\operatorname{\_1}, \operatorname{\_1}\bigr\rbrack, x, 3)Out[[[x,x],[x,x]],[[x,x],[x,x]]]\bigl\lbrack\bigl\lbrack\bigl\lbrack x, x\bigr\rbrack, \bigl\lbrack x, x\bigr\rbrack\bigr\rbrack, \bigl\lbrack\bigl\lbrack x, x\bigr\rbrack, \bigl\lbrack x, x\bigr\rbrack\bigr\rbrack\bigr\rbrack

The reflected binary Gray code: each step appends a shifted reversal

live ↗
InNest(_1⁡↦Join(_1⁡,Length(_1⁡)+Reverse(_1⁡)),[0],4)\mathrm{Nest}(\operatorname{\_1}↦\mathrm{Join}(\operatorname{\_1}, \mathrm{Length}(\operatorname{\_1})+\mathrm{Reverse}(\operatorname{\_1})), \bigl\lbrack0\bigr\rbrack, 4)Out[0,1,3,2,6,7,5,4,12,13,15,14,10,11,9,8]\bigl\lbrack0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8\bigr\rbrack
About
Examples17
Checked againstWolfram Language 17 agreemathlib4 5 agree
CrosswalkWolfram Language

Implementation

mappedmathlib4packages/oracle/src/mappings.ts

A negative or non-integer count fails to elaborate, where ours holds the call.

See also: NestList, FixedPoint