RE: NestWhile
- To: mathgroup at smc.vnet.net
- Subject: [mg32546] RE: [mg32519] NestWhile
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Sat, 26 Jan 2002 04:08:10 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
> -----Original Message----- > From: Tom De Vries [mailto:tdevries at shop.westworld.ca] To: mathgroup at smc.vnet.net > Sent: Friday, January 25, 2002 8:58 AM > To: mathgroup at smc.vnet.net > Subject: [mg32546] [mg32519] NestWhile > > > Hello! > > I know this is a simple question, hope someone can help me out..... > > If I define the function > > f[n_] := If[EvenQ[n], n/2, 3n + 1] > > > I can then apply NestList and generate a nice number loop...... > > NestList[f, 7, 23] > > I want to use NestWhileList and stop when the generated > number is a 1. How > do I do that? > > Thanks for the help, > > Sincerely, > > Tom De Vries > > Tom, you might search for NestWhileList[f, 7, {4, 2, 1} =!= {##} &, 3] {7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1} or use just NestWhileList[f, 7, # > 1 &] (the only differ for n=1 and n=2) No you can do interesting things like TableForm[ Partition[(#1 -> Length[NestWhileList[f, #1, # > 1 &]] &) /@ Range[200], 5], TableSpacing -> {1, 1}] ListPlot[Length[NestWhileList[f, #, # > 1 &]] & /@ Range[2000]] ListPlot[Max[NestWhileList[f, #, # > 1 &]] & /@ Range[2000]] pl = ListPlot[NestWhileList[f, #, # > 1 &], PlotJoined -> True, PlotRange -> All, DisplayFunction -> Identity] & /@ (Range[#1, #1 - 2 #2 + 1, -1] &)[2^19 - 1, 5]; Show[GraphicsArray[Partition[pl, 2]]] -- Hartmut