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

New Free Tools on Dev Shed!

#1
December 9th, 2012, 04:50 PM
 FloridaSquirrel
Registered User

Join Date: Dec 2012
Posts: 2
Time spent in forums: 47 m 45 sec
Reputation Power: 0
Help me get started

Hi everyone --

I'm getting started with Python using the MIT Openware Course that I'm sure many of you have seen.

One of the first assignments is to create code that generates prime numbers.

I am having difficulty figuring this out. Here is what I have:
Code:
```#Makes a list of prime numbers between 3 and 10)
for candidate in range(3, 10):
divisor=1
while divisor<candidate-1:
divisor=divisor+1
remainder=candidate%divisor
if remainder==0:
print candidate, "divides evenly by", divisor
if remainder!=0:
print candidate, "does not divide evenly by", divisor```

From this output, I can figure out which are primes, but how do I gather together the information about the numbers that do not divide evenly by ANYTHING? I feel like I need another embedded loop here but am not sure how to do it.

I know there are dozens of different codes for finding primes online, but I am not interested in copying somebody's else's, I'd like to see the steps of how I can build my own program.

This is not homework, so don't feel guilty about helping me! I am a literature teacher learning Python on my own for pleasure

#2
December 9th, 2012, 05:17 PM
 AndrewSW
JavaScript is not spelt java

Join Date: Feb 2011
Location: Landan, England
Posts: 743
Time spent in forums: 2 Weeks 1 Day 23 h 14 m 42 sec
Reputation Power: 164
You don't need another loop, you just need a flag to keep track of whether any remainders are zero, for each candidate. The flag is reset to True for each candidate, and set to False if it is found to be divisible by something.

Code:
```#Makes a list of prime numbers between 3 and 20)
for candidate in range(3, 20):
divisor = 1
isPrime = True
while divisor < candidate - 1:
divisor = divisor + 1
remainder = candidate % divisor
if remainder == 0:
#print(candidate, "divides evenly by", divisor)
isPrime = False
else:
#print(candidate, "does not divide evenly by", divisor)
pass
if isPrime:
print("This is a prime: ", candidate)```

Quote:
 This is a prime: 3 This is a prime: 5 This is a prime: 7 This is a prime: 11 This is a prime: 13 This is a prime: 17 This is a prime: 19

Be liberal with spaces, it makes code easier to read and spot mistakes;
You don't need a 2nd 'if remainder', just else;
Python3 print() requires brackets so you might as well include them.

Make the docs your constant companion

Last edited by AndrewSW : December 9th, 2012 at 05:25 PM.

#3
December 9th, 2012, 05:36 PM
 AndrewSW
JavaScript is not spelt java

Join Date: Feb 2011
Location: Landan, England
Posts: 743
Time spent in forums: 2 Weeks 1 Day 23 h 14 m 42 sec
Reputation Power: 164
I tend to use Komodo Edit for Python but there are many alternatives.
Attached Images
 komodo71.png (72.5 KB, 39 views)

#4
December 9th, 2012, 06:40 PM
 FloridaSquirrel
Registered User

Join Date: Dec 2012
Posts: 2
Time spent in forums: 47 m 45 sec
Reputation Power: 0
You guys are awesome! After getting your help, I was able to do the second stage of the task, to compute the 1000th prime number. I'm sure this isn't the most elegant way of doing it, but at least it works...

Code:
```#Finds the 1000th prime number
number_in_sequence=1 #because 2 itself is the first prime and we won't test it
for candidate in range(3, 100000, 2):#start with 3, stop at a big number, test odd numbers only
divisor=1
isPrime = True#create variable isPrime
while divisor < candidate-1: #test all possible divisors lower than the candidate
divisor = divisor + 1 #keep increasing the divisor to test them all
remainder = candidate % divisor #remainder tells us if it's prime
if remainder == 0:
##print candidate, "divides evenly by", divisor
isPrime = False #tells us it can't be prime, loops
##print candidate, "is not a prime"
else:
##print candidate, "does not divide evenly by", divisor
if isPrime == True:
pass
if isPrime == True:
number_in_sequence = number_in_sequence+1
##print candidate, "is a prime number.  It's no. ", number_in_sequence
if number_in_sequence == 1000:
print candidate,"is the thousandth prime number."
break```

#5
December 9th, 2012, 06:51 PM
 AndrewSW
JavaScript is not spelt java

Join Date: Feb 2011
Location: Landan, England
Posts: 743
Time spent in forums: 2 Weeks 1 Day 23 h 14 m 42 sec
Reputation Power: 164
Try to keep your inline comments further to the right (or preferably on separate lines) as they obscure the code.

[I didn't know they had squirrels in Florida? Perhaps they were imported..]

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help me get started

## Developer Shed Advertisers and Affiliates

 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 Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

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