Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"this': 0.03; 'syntax': 0.04; 'odd': 0.07; 'remaining': 0.07; 'correct,': 0.09; 'logic': 0.09; 'python': 0.11; 'itself.': 0.14; 'books': 0.15; '"is': 0.16; '1000):': 0.16; '2),': 0.16; '>on': 0.16; '>the': 0.16; 'count,': 0.16; 'excludes': 0.16; 'exit.': 0.16; 'itself).': 0.16; 'list):': 0.16; 'multiplies': 0.16; 'requested.': 0.16; 'true:': 0.16; 'wrote:': 0.18; '(not': 0.18; 'library': 0.18; 'pointed': 0.19; '(the': 0.22; 'input': 0.22; 'import': 0.22; 'to:name:python- list@python.org': 0.22; 'print': 0.22; '(a)': 0.24; 'math': 0.24; "haven't": 0.24; '>': 0.26; 'header:In-Reply-To:1': 0.27; '[2]': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'default,': 0.31; 'factor': 0.31; 'sep': 0.31; 'received:209.85.212': 0.32; 'fri,': 0.33; 'received:209.85': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'done,': 0.36; 'doing': 0.36; 'url:org': 0.36; 'example,': 0.37; 'list': 0.37; 'received:209': 0.37; 'step': 0.37; 'being': 0.38; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'heard': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'problems.': 0.60; 'then,': 0.60; 'break': 0.61; 'numbers': 0.61; 'skip:n 10': 0.64; 'prime': 0.74; 'divide': 0.84; 'url:2014': 0.84; 'composite': 0.91; 'url:16': 0.93 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=ynocqjJDmoJyjGXHyvUTNmFlTAWO9WJNsoWKRveyhxw=; b=jionK93uje64ZuGqCrbE1/+hD3gu/vqeCn0rO9UlJc7r1Q+dHl5QAWelCjBBFS61Lp XTf8pc0wvemyBnXOKIhnscw18zgLmEmSiZ5gmtg03lA+aYcUy15czNedfAyxec6ojYlu 2gW9Cr/1h5eVnBzw63cPAFP1E25R8W88PVWIH3z/5P1LdugzmYRCJbOarOW58xblOLBq X515GRQMYHkVgwyp+I+tdLTql9pTCx4UTX5zDsH1TEhTNNWo4TCbR3NIBdkH8mfhhdYY hdZN+Lz7f6Tg6tc7iaVc1Xbqr/f7jUz/vowCk6ziFHni3JM6zjidyORev/cFCUMGc5gb YPeQ== X-Gm-Message-State: ALoCoQlTapEKlShCKFMiQdlD0FBXZS556d++3Q14tWkUzocY7DF7qhHrnbeY1TpVzwvKVD1B+z4C X-Received: by 10.180.8.230 with SMTP id u6mr6512718wia.24.1409955302839; Fri, 05 Sep 2014 15:15:02 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <6ebk0a95ia9llot967jf7hpmidhh6hl4fd@4ax.com> References: <1enj0att6bkrnvb81rhma5dbuk3h28agl8@4ax.com> <6ebk0a95ia9llot967jf7hpmidhh6hl4fd@4ax.com> From: Chris Kaynor Date: Fri, 5 Sep 2014 15:14:41 -0700 Subject: Re: My backwards logic To: "python-list@python.org" Content-Type: multipart/alternative; boundary=f46d04428628f9ab06050258cc62 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 140 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409955304 news.xs4all.nl 2969 [2001:888:2000:d::a6]:35845 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77621 --f46d04428628f9ab06050258cc62 Content-Type: text/plain; charset=UTF-8 On Fri, Sep 5, 2014 at 2:49 PM, Seymore4Head wrote: > On Fri, 05 Sep 2014 12:48:56 -0400, Seymore4Head > wrote: > > >I'm still doing practice problems. I haven't heard from the library > >on any of the books I have requested. > > > > > http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html > > > >This is not a hard problem, but it got me to thinking a little. A > >prime number will divide by one and itself. When setting up this > >loop, if I start at 2 instead of 1, that automatically excludes one of > >the factors. Then, by default, Python goes "to" the chosen count and > >not "through" the count, so just the syntax causes Python to rule out > >the other factor (the number itself). > > > >So this works: > >while True: > > a=random.randrange(1,8) > > print (a) > > for x in range(2,a): > > if a%x==0: > > print ("Number is not prime") > > break > > wait = input (" "*40 + "Wait") > > > >But, what this instructions want printed is "This is a prime number" > >So how to I use this code logic NOT print (not prime) and have the > >logic print "This number is prime" > > I am sure this has already been done, but after it was pointed out > that you don't need to test for any number that multiplies by 2 it > made me think again. > > If you start with the list [3,5,7] and step through the list of all > remaining odd numbers (step 2), and start appending numbers that won't > divide by numbers already appended in the list, that would seem like a > pretty efficient way to find all prime numbers. > To be correct, you only need to check for n being divisible by primes less than sqrt(n). For example, the following code will produce a list of primes from 2 to 1000 (the result will be in the "primes" list): import math primes = [2] for i in range(3, 1000): end = math.sqrt(i) for x in primes: if x > end: # Once x is larger than the sqrt(i), we know it must be prime, so we can early exit. #print(i, "is a prime number") primes.append(i) break if (i % x) == 0: #print(i, "is a composite number") break --f46d04428628f9ab06050258cc62 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On F= ri, Sep 5, 2014 at 2:49 PM, Seymore4Head <Seymore4Head@hotmail.= invalid> wrote:
On Fri, 05 Sep 2014 12:48:56 -0400, Seymore4Head
<Seymore4Head@Hotmail.invalid> wrote:

>I'm still doing practice problems.=C2=A0 I haven't heard from t= he library
>on any of the books I have requested.
>
>http://www.practicepython.org/ex= ercise/2014/04/16/11-check-primality-functions.html
>
>This is not a hard problem, but it got me to thinking a little.=C2=A0 A=
>prime number will divide by one and itself.=C2=A0 When setting up this<= br> >loop, if I start at 2 instead of 1, that automatically excludes one of<= br> >the factors.=C2=A0 Then, by default, Python goes "to" the cho= sen count and
>not "through" the count, so just the syntax causes Python to = rule out
>the other factor (the number itself).
>
>So this works:
>while True:
>=C2=A0 =C2=A0 a=3Drandom.randrange(1,8)
>=C2=A0 =C2=A0 print (a)
>=C2=A0 =C2=A0 for x in range(2,a):
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 if a%x=3D=3D0:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 print ("Number is not pr= ime")
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break
>=C2=A0 =C2=A0 wait =3D input (" "*40=C2=A0 + "Wait"= )
>
>But, what this instructions want printed is "This is a prime numbe= r"
>So how to I use this code logic NOT print (not prime) and have the
>logic print "This number is prime"

I am sure this has already been done, but after it was pointed = out
that you don't need to test for any number that multiplies by 2 it
made me think again.

If you start with the list [3,5,7] and step through the list of all
remaining odd numbers (step 2), and start appending numbers that won't<= br> divide by numbers already appended in the list, that would seem like a
pretty efficient way to find all prime numbers.

To be correct, you only need to check for n being divisible by pri= mes less than sqrt(n). For example, the following code will produce a list = of primes from 2 to 1000 (the result will be in the "primes" list= ):

import math
primes =3D [2]<= /div>
for i in range(3, 1000):
=C2=A0 =C2=A0 end =3D math.sqr= t(i)
=C2=A0 =C2=A0 for x in primes:
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 if x > end: # Once x is larger than the sqrt(i), we know it m= ust be prime, so we can early exit.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 #print(i, "is a prime number")
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 primes.append(i)
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 break
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if = (i % x) =3D=3D 0:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 #prin= t(i, "is a composite number")
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 break

--f46d04428628f9ab06050258cc62--