Re: NestWhile

• To: mathgroup at smc.vnet.net
• Subject: [mg23060] Re: [mg23039] NestWhile
• From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
• Date: Sat, 15 Apr 2000 03:00:21 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```on 4/13/00 3:43 PM, Alan W.Hopper at awhopper at hermes.net.au wrote:

> Others like myself who are still using Mathematica 3,  may have been
> interested to see that the new cryptic @@@ function can be programmed
> into a user function for version 3, as explained by Hartmut Wolf and
> Daniel Reeves.
>
> But I have a query to pose about another new version 4 function  ,
> that is NestWhile, which I assume can also be approximated in version
> 3.0.
>
>
> This code was taken from Eric Weisstein's IntegerSequences.m package,
> obtained from the mathworld.wolfram site ;
>
> KeithNumberQ[n_Integer?Positive]:=(KeithSequence[n][[-1]] == n)
>
> KeithSequence[n_Integer?Positive]:= Module[{d = IntegerDigits[n], l},
> l = Length[d];
> NestWhile[Append[#, Plus @@ Take[#,-l]] &, d, #[[-1]] < n &]
> ]
>
>
> The first Keith Number is 197 and it's Keith Sequence should be
> {1, 9, 7, 17, 33, 57, 107, 197},  or similar.
>
> as starting with the digits of 197 ,
> 1 + 9 + 7 = 17
> 9 + 7 + 17 = 33
> 7 + 17 + 33 = 57
> 17 + 33 + 57 = 107
> 33 + 57 + 107 = 197
>
> Other small Keith Numbers are  742, 1104, 1537, 2208,
> a large one is 97295849958669 .
> (see Don Piele - Mathematica Pearls - Mathematica in Research and
> Education - Vol 6 No 3 , p 50,  also  Vol 7 No 1, p 45.
>
>
> So the question is how can a Mathematica 3 version of NestWhile be
> substituted into the above code?
>
>
>
> Alan Hopper
>
> awhopper at hermes.net.au
>
>
Actually, NestWhile has a lot of forms than is needed for KeithNumbers:

In[1]:=
?NestWhile
NestWhile[f, expr, test] starts with expr, then repeatedly

applies f until applying test to the result no longer

yields True. NestWhile[f, expr, test, m] supplies the most

recent m results as arguments for test at each step.

NestWhile[f, expr, test, All] supplies all results so far

as arguments for test at each step. NestWhile[f, expr,

test, m, max] applies f at most max times. NestWhile[f,

expr, test, m, max, n] applies f an extra n times.

NestWhile[f, expr, test, m, max, -n] returns the result

found when f had been applied n fewer times.

Here is one possible implementation of the first three forms.

In[2]:=
MyNestWhile[f_, expr_, test_, n_Integer?Positive] :=
Module[{l = NestList[f, expr, n - 1]},
FixedPoint[If[Apply[test, l], l = Append[Rest[l], f[#]]; f[#], #] &,
Last[l]]]

In[3]:=
MyNestWhile[f_, expr_, test_] := MyNestWhile[f, expr, test, 1]

In[4]:=
MyNestWhile[f_, expr_, test_, All] :=
Module[{l = {expr}},
FixedPoint[If[Apply[test, l], l = Append[l, f[#]]; f[#], #] &, Last[l]]]

We can check that this works with Keith numbers:

In[5]:=
KeithNumberQ[n_Integer?Positive] := (KeithSequence[n][[-1]] == n)

In[6]:=
KeithSequence[n_Integer?Positive] := Module[{d = IntegerDigits[n], l},
l = Length[d];
MyNestWhile[Append[#, Plus @@ Take[#, -l]] &, d, #[[-1]] < n &]
]
In[7]:=
KeithNumberQ[197]
Out[7]=
True

In[8]:=
KeithSequence[197]
Out[8]=
{1, 9, 7, 17, 33, 57, 107, 197}

There are of course countless other ways of implementing this and I have not
considered the question of efficiency at all.

--
Andrzej Kozlowski
Toyama International University
JAPAN

```

• Prev by Date: Re: NestWhile
• Next by Date: Re: Mesh
• Previous by thread: Re: NestWhile
• Next by thread: Re: NestWhile