Re: Help with While Loop Function
- To: mathgroup at smc.vnet.net
- Subject: [mg115948] Re: Help with While Loop Function
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Thu, 27 Jan 2011 03:39:48 -0500 (EST)
Clearall should be ClearAll, but you probably need only Clear. You cleared x, y, t, w -- which don't need clearing -- and didn't Clear f, which might. You have several parentheses () where you need brackets [] in the definition of f. You have List1 most places, but list1 (no capital) is the first argument to Append. Append is slow; there are better ways. One is using list1 = {list1, t}, then list1 = Flatten@list1 after the While is complete. Better yet, use Sow and Reap. You have If[ f[t, y] = 0, (other arguments)] where "=" is an assignment, not a test for equality. Ditto for the other If. You have f[t_, y_] == (whatever), which does not set f equal to anything; it tests for equality. You don't need Floor to find out if something is a perfect square. Just test whether Sqrt gives an Integer. In the text, you mentioned testing if "t*((y^2+1)/2)^2 + y^2 is a perfect square" is a perfect square, but that's not what you're testing. Instead, you're testing (t/4) (y^2 - 1)^2 + y^2. w is the number of items added to the list, but that would be Length@list1 - 2. If[w == 3, t = limit, x++] is intended to exit the loop early while you're debugging? But you could use Break, making the code more clear (although I don't approve of using Break, Return, GoTo, and all that). Better yet, use a reasonable limit on t. If I've guessed right what you actually meant (not likely), the result is this: ClearAll[x, y, t, w, f] x = 2; y = 3; t = 1; w = 0; f[t_, y_] := IntegerQ@Sqrt [(t/4) (y^2 - 1)^2 + y^2] limit = 100000000000; (* 100 billion *) Timing[ list1 = Join[ {0, 1}, First@Last@Reap@While[ t < limit, t += x; f[t, y] && (Sow@t; w++); x++] ] ] {71.5941, {0, 1, 10, 45, 351, 1540, 11935, 52326, 405450, 1777555, 13773376, 60384555, 467889345, 2051297326, 15894464365, 69683724540}} That counts how many are added, but it doesn't Break. If you want the break, it could look like w == 3 && Break[]; as a new line before x++. Here's a longer run, not incrementing w: ClearAll[x, y, t, w, f] x = 2; y = 3; t = 1; w = 0; f[t_, y_] := IntegerQ@Sqrt [(t/4) (y^2 - 1)^2 + y^2] limit = 1000000000000; Timing[ list1 = Join[ {0, 1}, First@Last@Reap@While[ t < limit, t += x; f[t, y] && Sow@t; x++] ] ] {294.936, {0, 1, 10, 45, 351, 1540, 11935, 52326, 405450, 1777555, 13773376, 60384555, 467889345, 2051297326, 15894464365, 69683724540, 539943899076}} Solve[{n (n + 1)/2 == #, n > 0}, n, Integers] & /@ list1 {{}, {{n -> 1}}, {{n -> 4}}, {{n -> 9}}, {{n -> 26}}, {{n -> 55}}, {{n -> 154}}, {{n -> 323}}, {{n -> 900}}, {{n -> 1885}}, {{n -> 5248}}, {{n -> 10989}}, {{n -> 30590}}, {{n -> 64051}}, {{n -> 178294}}, {{n -> 373319}}, {{n -> 1039176}}} 0 is not. Bobby On Wed, 26 Jan 2011 04:04:26 -0600, KenR <ramsey2879 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 > -- DrMajorBob at yahoo.com