Re: Help with While Loop Function
- To: mathgroup at smc.vnet.net
- Subject: [mg115960] Re: Help with While Loop Function
- From: Peter Pein <petsie at dordos.net>
- Date: Thu, 27 Jan 2011 03:42:01 -0500 (EST)
- References: <ihorj6$hfh$1@smc.vnet.net>
On 26.01.2011 11:04, KenR 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) 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]; ... , AppendTo[List1,t]; w++ ... > If [w = 3,t = 1000000000000, x++]]; .. w== 3 .. > List1 > If I didn't miss anything, you want the Sophie Germain triangular numbers (http://oeis.org/A124174). This is a problem for which Reduce has been made: In[1]:= t /. {ToRules@ Reduce[t == n (n + 1)/2 && 2 t + 1 == m (m + 1)/2 && Element[{m, n, t}, Integers] && 0 <= t <= 10^12 && 0 <= n && 0 <= m, t, Backsubstitution -> True]} Out[1]= {0, 1, 10, 45, 351, 1540, 11935, 52326, 405450, 1777555, 13773376, 60384555, 467889345, 2051297326, 15894464365, 69683724540, 539943899076} In[2]:= SophieGermain[n_] = Collect[FindSequenceFunction[%, n] // RootReduce, _^n, Simplify] Out[2]= -(11/32) + 1/64 (-3 - 2 Sqrt[2])^n (1 - Sqrt[2]) + 5/64 (3 - 2 Sqrt[2])^n (2 + Sqrt[2]) + 1/64 (1 + Sqrt[2]) (-3 + 2 Sqrt[2])^n - 5/64 (-2 + Sqrt[2]) (3 + 2 Sqrt[2])^n hth, Peter