Re: Help with While Loop Function
- To: mathgroup at smc.vnet.net
- Subject: [mg116035] Re: Help with While Loop Function
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Sat, 29 Jan 2011 19:42:38 -0500 (EST)
Peter,
That is sheer GENIUS for y = 3, but it doesn't work for any other y that
I've tried.
First, here's a simplification of my earlier code:
limit = 100000000000; k = 2; y = 3;
expr = y^2 + t/4 (-1 + y^2)^2
Timing[list = {0, 1}~Join~
First@Last@Reap@
While[(t = k (k + 1)/2) < limit,
IntegerQ@Sqrt[expr] && Sow[t];
k++
]
]
9 + 16 t
{70.1258, {0, 1, 10, 45, 351, 1540, 11935, 52326, 405450, 1777555,
13773376, 60384555, 467889345, 2051297326, 15894464365, 69683724540}}
Collect[FindSequenceFunction[list, n] // RootReduce, _^n, Simplify]
-(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
But now, if we change y to 5:
limit = 10000000000; k = 2; y = 5;
expr = y^2 + t/4 (-1 + y^2)^2
Timing[list = {0, 1}~Join~
First@Last@Reap@
While[(t = k (k + 1)/2) < limit,
IntegerQ@Sqrt[expr] && Sow[t];
k++
]
]
25 + 144 t
{24.5298, {0, 1, 171, 1326, 197506, 1530375, 227921925, 1766051596}}
Collect[FindSequenceFunction[list, n] // RootReduce, _^n, Simplify]
FindSequenceFunction[{0, 1, 171, 1326, 197506, 1530375, 227921925,
1766051596}, n]
or y = 2:
limit = 10000000000; k = 2; y = 2;
expr = y^2 + t/4 (-1 + y^2)^2
Timing[list = {0, 1}~Join~
First@Last@Reap@
While[(t = k (k + 1)/2) < limit,
IntegerQ@Sqrt[expr] && Sow[t];
k++
]
]
4 + (9 t)/4
{16.2608, {0, 1, 276, 820, 319600, 947376, 368819220, 1093272180}}
Collect[FindSequenceFunction[list, n] // RootReduce, _^n, Simplify]
FindSequenceFunction[{0, 1, 276, 820, 319600, 947376, 368819220,
1093272180}, n]
I'm working on generalizing your trick with Reduce for other y. I'm not
clear on that, just yet.
Bobby
On Thu, 27 Jan 2011 02:42:01 -0600, Peter Pein <petsie at dordos.net> wrote:
> 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
>
>
--
DrMajorBob at yahoo.com