Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 8th, 2012, 03:45 AM
MightyGreek MightyGreek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 MightyGreek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 5 sec
Reputation Power: 0
Prime Number Engine (Sieve)

Sorry I have reached a point of desperation. The assignment is to "Write a program that prompts the user for an integer n, then generate and print a list of all the
primes between 2 and n using the Sieve of Eratosthenes". I have a a skeleton but I cannot figure out why I keep getting an error. Any help would be much appreciated.

Code:
Original - Code
    n=int(input("n? ")) x=[] for i in range(2,(n+1)):     x.append(i) primes=[2] p=2  while sum(x) != 0:     i=1     while i*p<=n:         x.remove(i*p)         i=i+1     p=x[0]     primes.append(p) print(primes)


Apologies, I have no idea how to format this correctly. Again, thanks so much.

Reply With Quote
  #2  
Old November 8th, 2012, 08:23 AM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 37 m 44 sec
Reputation Power: 2
What error are you getting?
When I try, I get an error at x.remove(i*p) when i=2 and p=3 that "x not in list". I'm not familiar with the sieve but you could trap that error with:
Code:
if i*p in x: x.remove(i*p)

Reply With Quote
  #3  
Old November 8th, 2012, 08:24 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
Scalar multiplication commutes.
Some integers have more than 2 prime factors.
Try this, it is much closer to working and displays its operation.
Code:
n=int(input("n? "))
x=[]
for i in range(2,(n+1)):
    x.append(i)
primes=[2]
p=2
while sum(x) != 0:
    i=1
    while i*p<=n:
        if i*p in x:
            x.remove(i*p)
            print('%d == %d*%d REMOVED'%(i*p,i,p))
        else:
            print('%d == %d*%d already gone'%(i*p,i,p))
        i=i+1
    if x:
        p=x[0]
    primes.append(p)
print(primes)
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #4  
Old November 8th, 2012, 08:44 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 38 m 3 sec
Reputation Power: 1568
The specific problem you are asking about is that you are trying to remove an item from a list which has already been removed in a previous pass. For example 6 is a multiple of both 2 and 3; when you test for multiples of 2, you remove it, then when you test for multiples of 3 you try to remove it again. You need to add a test for the presence of the tested composite number before trying to remove it:
Code:
        currentComposite = i * p
        if currentComposite in x:
            x.remove(currentComposite)


This is only one problem of several that I've found, but you are on the right track, overall.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Reply With Quote
  #5  
Old November 8th, 2012, 12:54 PM
MightyGreek MightyGreek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 MightyGreek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
Scalar multiplication commutes.
Some integers have more than 2 prime factors.
Try this, it is much closer to working and displays its operation.
Code:
n=int(input("n? "))
x=[]
for i in range(2,(n+1)):
    x.append(i)
primes=[2]
p=2
while sum(x) != 0:
    i=1
    while i*p<=n:
        if i*p in x:
            x.remove(i*p)
            print('%d == %d*%d REMOVED'%(i*p,i,p))
        else:
            print('%d == %d*%d already gone'%(i*p,i,p))
        i=i+1
    if x:
        p=x[0]
    primes.append(p)
print(primes)



Fantastic thank you so much. This got me working. I did not even consider that I was pulling out multiples and then reevaluating them.

I have one more conceptual question. For the line p=x[0], why must we put it after the if statement?

Thanks again!!!

Reply With Quote
  #6  
Old November 8th, 2012, 12:55 PM
MightyGreek MightyGreek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 MightyGreek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by rrashkin
What error are you getting?
When I try, I get an error at x.remove(i*p) when i=2 and p=3 that "x not in list". I'm not familiar with the sieve but you could trap that error with:
Code:
if i*p in x: x.remove(i*p)



I greatly appreciate the input. Thank you so much!

Reply With Quote
  #7  
Old November 8th, 2012, 12:57 PM
MightyGreek MightyGreek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 MightyGreek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by Schol-R-LEA
The specific problem you are asking about is that you are trying to remove an item from a list which has already been removed in a previous pass. For example 6 is a multiple of both 2 and 3; when you test for multiples of 2, you remove it, then when you test for multiples of 3 you try to remove it again. You need to add a test for the presence of the tested composite number before trying to remove it:
Code:
        currentComposite = i * p
        if currentComposite in x:
            x.remove(currentComposite)


This is only one problem of several that I've found, but you are on the right track, overall.


I did not even think about that. Thank you, I appreciate the input. I have to do a better job of visualizing what the program is actually running. Thanks again!

Reply With Quote
  #8  
Old November 8th, 2012, 01:16 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
Quote:
Originally Posted by MightyGreek
I have one more conceptual question. For the line p=x[0], why must we put it after the if statement?
Try the program without
if x:

When is a list True?
When is a list False?

Reply With Quote
  #9  
Old November 8th, 2012, 02:11 PM
MightyGreek MightyGreek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 MightyGreek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
Try the program without
if x:

When is a list True?
When is a list False?


I'm going to guess if there is nothing in the list and you try to evaluate the expression, you are returned with False? If that is the case, if x: allows us to use the list while it is True but stops, once the list is False. Am I close?

Reply With Quote
  #10  
Old November 8th, 2012, 02:34 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
You could read about it.

You could try it in the interpreter

not not []

not not [1,2,3]

Last edited by b49P23TIvg : November 8th, 2012 at 02:36 PM.

Reply With Quote
  #11  
Old November 8th, 2012, 02:54 PM
MightyGreek MightyGreek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 MightyGreek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
You could read about it.

You could try it in the interpreter

not not []

not not [1,2,3]


I see, thank you so much for the patience. As you can see, I have some significant gaps. I will definitely work on them, but again, thank you for taking the time to help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Prime Number Engine (Sieve)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap