Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Complement replacement

  • To: mathgroup at smc.vnet.net
  • Subject: [mg58624] Re: Complement replacement
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sun, 10 Jul 2005 16:51:46 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, England
  • References: <daqosi$6d8$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

konstantpi at mail15.com wrote:
> hi
> in the list:
> pp=Table[Random[Integer, {1, 1000}], {i, 1000}];
> how could i know which numbers from 1 to 1000 does not exist in the 
> pp List.
> but without using:
> Complement[Table[i,{i,1000}],pp]
> regards
> 
Hello,

I am not sure to understand why you would not like to use such an 
efficient function as *Complement* to solve your problem (homework 
assignment perhaps?); anyway rather than a functional approach such as

In[1]:=
pp = Table[Random[Integer, {1, 10}], {i, 10}]

Out[1]=
{9, 1, 8, 4, 7, 2, 8, 3, 10, 8}

In[2]:=
Complement[Range[10], pp]

Out[2]=
{5, 6}

you could use a procedural design for instance

In[3]:=
res = {};
i = 1;
While[i <= Length[pp], j = 1; notInPP = True;
    While[j <= Length[pp] && notInPP == True,
      If[i == pp[[j]], notInPP = False]; j++; ]*
     If[notInPP == True, AppendTo[res, i]]; i++; ]
res

Out[6]=
{5, 6}

Now if you are looking for speed of execution (and also of coding), the 
functional approach is definitely better and much much faster. For example,

In[7]:=
pp = Table[Random[Integer, {1, 1000}], {i, 1000}];

In[8]:=
Timing[Complement[Range[Length[pp]], pp]; ]

Out[8]=
{0.*Second, Null}

In[9]:=
Timing[res = {}; i = 1; While[i <= Length[pp],
      j = 1; notInPP = True;
       While[j <= Length[pp] && notInPP == True,
         If[i == pp[[j]], notInPP = False]; j++; ]*
        If[notInPP == True, AppendTo[res, i]]; i++; ]*
     res; ]

Out[9]=
{4.141*Second, Null}

and here with a code slightly optimized (we sort the list pp and discard 
any duplicates before the main loop and also we use the functions *Reap* 
and *Sow* rather than *AppendTo*)

In[10]:=
Timing[i = 1; pp = Union[pp];
    Reap[While[i <= 1000, j = 1; notInPP = True;
       While[j <= Length[pp] && notInPP == True,
         If[i == pp[[j]], notInPP = False]; j++; ]*
        If[notInPP == True, Sow[i]]; i++; ]]; ]

Out[10]=
{2.8120000000000003*Second, Null}

In[11]:=
$Version

Out[11]=
"5.1 for Microsoft Windows (January 27, 2005)"

Hope this helps,
/J.M.


  • Prev by Date: Re: Re: Complement replacement
  • Next by Date: Re: Re: Updated my RootSearch package
  • Previous by thread: Re: Re: Complement replacement
  • Next by thread: Re: Complement replacement