Re: Testing for squares
- To: mathgroup at smc.vnet.net
- Subject: [mg66245] Re: Testing for squares
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Fri, 5 May 2006 05:02:47 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 5/4/06 at 5:20 AM, julian.aguirre at ehu.es (Julian Aguirre) wrote: >2) Let MyList be a large list of integers (somewhere between 20.000 >and 50.000 elements) and MyTest a function on the integers returning >True or False (for instance, test2 above). What is an efficient way >of deciding if there is an element in MyList for which MyTest is >True? Right now I use >Select[MyList, MyTest, 1] != {} There are a variety of alternatives particularly if your function isn't simply restricted to returning True or False. For example consider trying to determine if a given value occurs in a list of random integers. In[53]:= data = Table[Random[Integer, {1, 10^6}], {10^5}]; then any of the following could be used In[59]:= Timing[Count[data, 100]] Out[59]= {0.053191 Second,0} In[55]:= Timing[Select[data,100==#&]] Out[55]= {0.27063 Second,{}} In[56]:= Timing[Cases[data,100]] Out[56]= {0.053879 Second,{}} In[57]:= Timing[Pick[data,#==100&/@data]] Out[57]= {0.348369 Second,{}} The fastest method I know for this specific problem is: In[58]:= Timing[SparseArray[1 - Abs[Sign[data - 100]]] /. SparseArray[_, _, _, a_] :> Last[a]] Out[58]= {0.014741 Second, {}} Note, the fastest method will likely change with details of the function used as the test function. -- To reply via email subtract one hundred and four