RE: NestWhile
- To: mathgroup at smc.vnet.net
- Subject: [mg32552] RE: [mg32519] NestWhile
- From: "David Park" <djmp at earthlink.net>
- Date: Sat, 26 Jan 2002 04:08:22 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Hi Tom, f[n_] := If[EvenQ[n], n/2, 3n + 1] I don't think that you want NestWhile because all it will return is 1. I assume that you would want to see the sequence or at least the length of the run. The following statement chops off at 50 applications of f because I don't know if you can get very long runs. NestWhileList[f, 9, # != 1 &, 1, 50] {9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1} This generates the runs for the first 50 integers and then displays the length of the run and whether it was good or bad. runs = NestWhileList[f, #, # != 1 &, 1, 100] & /@ Range[50]; {Length[#], If[Last[#] == 1, Good, Bad]} & /@ runs {{1, Good}, {2, Good}, {8, Good}, {3, Good}, {6, Good}, {9, Good}, {17, Good}, {4, Good}, {20, Good}, {7, Good}, {15, Good}, {10, Good}, {10, Good}, {18, Good}, {18, Good}, {5, Good}, {13, Good}, {21, Good}, {21, Good}, {8, Good}, {8, Good}, {16, Good}, {16, Good}, {11, Good}, {24, Good}, {11, Good}, {101, Bad}, {19, Good}, {19, Good}, {19, Good}, {101, Bad}, {6, Good}, {27, Good}, {14, Good}, {14, Good}, {22, Good}, {22, Good}, {22, Good}, {35, Good}, {9, Good}, {101, Bad}, {9, Good}, {30, Good}, {17, Good}, {17, Good}, {17, Good}, {101, Bad}, {12, Good}, {25, Good}, {25, Good}} I think this is one of those problems in which it is very difficult to determine in general if and when it will stop. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ > From: Tom De Vries [mailto:tdevries at shop.westworld.ca] To: mathgroup at smc.vnet.net > > 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 > >