Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: NestWhile

  • To: mathgroup at smc.vnet.net
  • Subject: [mg23055] Re: [mg23039] NestWhile
  • From: Hartmut Wolf <hwolf at debis.com>
  • Date: Sat, 15 Apr 2000 03:00:18 -0400 (EDT)
  • Organization: debis Systemhaus
  • References: <200004130643.CAA24830@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Alan W.Hopper schrieb:
> 
> 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?
> 

Dear Alan,

NestWhile is a function with a plentiful interface, so it would cost
some effort to mimik it. However the candidate for a substitution in
Version 3 is FixedPoint and use SameTest for exiting. 

For the Keith numbers I'd propose

In[10]:= 
KeithNumberQ[n_Integer?Positive] := (KeithSequence2[n][[-1]] == n)

In[11]:= KeithSequence[n_Integer?Positive] := 
  Module[{d = IntegerDigits[n], l}, l = Length[d];
      FixedPointList[Append[#, Plus @@ Take[#, -l]] &, d, 
        SameTest -> (! #[[-1]] < n &)]][[-2]]


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

In[13]:= KeithNumberQ[197]

Out[13]= True

So negate the NestWhile condition for SameTest, but when FixedPoint then
stops it has run one step too far, so get one back in ListFixedPoint. I
have not the time now, but you may continue that reasoning for building
up the full interface.

Kind regards, Hartmut


  • References:
    • NestWhile
      • From: "Alan W.Hopper" <awhopper@hermes.net.au>
  • Prev by Date: problem with Integrate
  • Next by Date: Re: NestWhile
  • Previous by thread: NestWhile
  • Next by thread: Re: NestWhile