MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Challenge: Fastest method to convert positive integers to 1 in a long list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg52195] Re: Challenge: Fastest method to convert positive integers to 1 in a long list
  • From: Peter Pein <petsie at arcor.de>
  • Date: Sun, 14 Nov 2004 04:31:00 -0500 (EST)
  • References: <cn4m4e$19p$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Carl K. Woll wrote:
> Hi all,
> 
> Inspired by the recent thread on counting runs, I have the following 
> challenge. Come up with a method to convert all the positive integers in a 
> long sequence of nonnegative integers to 1, so that the sequence consists of 
> only 0s and 1s. Let the sequence be given by
> 
> seq = Table[Random[Integer, 10], {10^6}];
> 
> Then, one technique is
> 
> newseq = 1+Quotient[#,#+1,1]&@seq;
> 
> Can anyone do better?
> 
> Carl Woll 
> 
> 

Sign[.] is simpler, isn't it?

In[1]:=  seq = Table[Random[Integer, 10], {10^6}];

In[2]:=  t1 = First@Timing[newseq1 = 1 + Quotient[#, # + 1, 1] &@seq;]
Out[2]=  0.671 Second

In[3]:=  t2 = First@Timing[newseq2 = Sign@seq;]
Out[3]=  0.12 Second

In[4]:=  newseq1 == newseq2
          t1/t2
Out[4]=  True
Out[5]=  5.59167

..it is :-)
-- 
Peter Pein
Berlin


  • Prev by Date: Re: Challenge: Fastest method to convert positive integers to 1 in a long list
  • Next by Date: Re: Challenge: Fastest method to convert positive integers to 1 in a long list
  • Previous by thread: Re: Challenge: Fastest method to convert positive integers to 1 in a long list
  • Next by thread: Re: Challenge: Fastest method to convert positive integers to 1 in a long list