[Date Index]
[Thread Index]
[Author Index]
Re: Can This Be Made Cleaner
*To*: mathgroup at smc.vnet.net
*Subject*: [mg29813] Re: [mg29797] Can This Be Made Cleaner
*From*: Andrzej Kozlowski <andrzej at tuins.ac.jp>
*Date*: Wed, 11 Jul 2001 01:27:02 -0400 (EDT)
*Sender*: owner-wri-mathgroup at wolfram.com
While it does make some sense it is not a very good approach. The main
problem is that you are making your list first and then running your search
over it. This means that when your list is much longer than necessary you
are wasting time computing it.
Here is an example.
In[1]:=
a[1] =5;
In[2]:=
a[n_]:= a[n] = (-1)^(n+1) (Abs[a[n-1]]+2)
In[3]:=
findD[a_,b_]:=Module[{n=a, lst=b},
i=1;
While[JacobiSymbol[lst[[i]],n]
!= -1, i++];lst[[i]]]
In[4]:=
num=567134594567891251143216731;
In[5]:=
((c=Table[a[n],{n,1,1000}];
findD[num,c]))//Timing
Out[5]=
{0.183333 Second,-15}
This took much longer then necessary because D was found very early on in
the list. On the the other hand if your list is too short:
In[6]:=
((c=Table[a[n],{n,1,5}];
findD[num,c]))//Timing
Part::partw: Part 6 of {5,-7,9,-11,13} does not exist.
Part::partw: Part 6 of {5,-7,9,-11,13} does not exist.
Out[6]=
{0.0833333 Second,{5,-7,9,-11,13}\[LeftDoubleBracket]6\[RightDoubleBracket]}
The following code avoids this problem:
In[7]:=
findD2[n_]:=
Module[{c=0},NestWhile[(c++;(-1)^c(Abs[#]+2))&,5,JacobiSymbol[#,n]!=-1&]]
In[8]:=
findD2[num]//Timing
Out[8]=
{0. Second,-15}
This satisfies your requirements except the one about the giving up
parameter. here is a modification that does that too.
In[9]:=
findD2[n_,t_]:=
TimeConstrained[
Module[{c=0},NestWhile[(c++;(-1)^c(Abs[#]+2))&,5,JacobiSymbol[#,n]!=-1&]],
t,"Could not find D after "<>ToString[c]<>" iterations"]
Here t is the time to give up if D has not been found after t seconds. (It
seems to me, however, that unless the Extended Riemann Hypothesis is false
you probably will never need it).
--
Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/
http://sigma.tuins.ac.jp/~andrzej/
on 01.7.11 9:25 AM, Flip at safebunch.com at Flip at safebunch.com wrote:
> Hello,
>
> I would like to move the sequence into the module and would like to add a max
> parameter to give up by (for example, if it doesn't find d by some time, print
> out cold not converge after max iterations).
>
> Can this be made easier, cleaner and faster with the sequence embedded within
> the module and it progresses the module one step at a time until it finds the
> appropriate D.
>
> Does it make sense to do it this way?
>
> Here is the short code.
>
> In[37]:=
> Clear[a]
>
> In[38]:=
> a[1] =5;
>
> In[39]:=
> a[n_]:= a[n] = (-1)^(n+1) (Abs[a[n-1]]+2)
>
> In[40]:=
> (*find the first D in a[n] for which JS[a,n] = -1 *)
>
> In[41]:=
> findD[a_,b_]:=Module[{n=a, lst=b},
> i=1;
> While[JacobiSymbol[lst[[i]],n]
> != -1, i++];lst[[i]]]
>
> In[56]:=
> num=3559713579543216731;
>
> In[52]:=
> c=Table[a[n],{n,1,100}];
>
> In[57]:=
> findD[num,c]
>
> Out[57]=
> -7
>
> Thank you for any inputs ... Wilson
>
>
>
Prev by Date:
**Re: Saving the whole workspace...**
Next by Date:
**fills for barchart**
Previous by thread:
**Re: Can This Be Made Cleaner**
Next by thread:
**Fitting multiple nonlinear equations to unequal length datasets**
| |