Re: Help with While Loop Function
- To: mathgroup at smc.vnet.net
- Subject: [mg115954] Re: Help with While Loop Function
- From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
- Date: Thu, 27 Jan 2011 03:40:54 -0500 (EST)
- References: <ihorj6$hfh$1@smc.vnet.net>
Hi, I am afraid you have created a long list of errors. May I suggest you try to read somewhat more documentation before going on? Anyways, here's the list with errors and tips and a working version: 1. Clearall should be ClearAll (capital A) 2. you might consider suppressing output on the next 5 lines using ; 3. "f[t_,y_] ==" should be "f[t_,y_] =". Single = is assignment (function Set[ ]), double = (==) is equality test (function Equal[]) 4. functions need square brackets to hold their arguments, not round parenthesis (Floor[], Sqrt[]) 5. Use := (SetDelayed) instead of = (Set) in the definition of f. = o= r Set uses the values of your variables at the time of the function definition if they have a value. This is not what you want here. 6. "If[f[t,y] = 0" must be "If[f[t,y] == 0"; a Set/Equal confusion 7. "List1 = Append[list1,t]" is OK, but you could use the more concise "AppendTo[list1,t]" instead. Both are relatively slow, so for long list other constructions would be better. 8. "If [w = 3" should be "If [w == 3"; a Set/Equal confusion 9. "t = 1000000000000" is used to get out of the loop. Break[] is invented to do just this in a more elegant way. (It's ugly nevertheless) So, we end up with: In[108]:= ClearAll[x, y, t, w] x = 2; y = 3; t = 1; w = 0; List1 = {0, 1}; f[t_, y_] := (Floor[ Sqrt[(t/4) (y^2 - 1)^2 + y^2]])^2 - ((t/4) (y^2 - 1)^2 + y^2); While[t < 1000000000000, t = t + x; If[f[t, y] == 0, List1 = Append[List1, t]; w = w + 1]; If[w == 3, Break[], x++]]; List1 Out[116]= {0, 1, 10, 45, 351} Cheers -- Sjoerd On Jan 26, 11:04 am, KenR <ramsey2... at msn.com> wrote: > Please Help me to Get the following to work as desired > t is the triangular number. I increment it repeated ly by adding the > counter x to it. When t*((y^2+1)/2)^2 + y^2 is a perfect square i want > to append t to my list of trangular numbers with the list starting > with {0,1}, and exit the loop when 3 more triangular numbers have been > added or when t >= 1000000000000. It is not working for me. I am new > to Mathematica. Thanks > > Clearall[x,y,t,w] > x = 2 > y = 3 > t = 1 > w = 0 > List1 = {0,1} > f[t_,y_] ==(Floor(Sqrt ((t/4) (y^2-1)^2 + y^2)))^2 - ((t/4) (y^2-1)^2 > + y^2) > While[t<1000000000000,t = t+x;If[f[t,y] = 0, List1 = Append[list1,t= ];w > = w +1]; > If [w = 3,t = 1000000000000, x++]]; > List1