Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 26 Online
OpenStudy (anonymous):

Hi there. Here is my attempt at the first problem in set #1. It's not working the way I want and would very much appreciate some guidance. Thanks in advance :) testmax = int(raw_input("Enter the maximum number to test: ")) while (testmax > 1): if (testmax % 2) == 1: #this gets me all odd numbers under testmax divisor = testmax - 1 #this gets me all integers under testmax as divisors while (divisor > 0): if (testmax % divisor) == 0: #if this is true then testmax is divisible by divisor print testmax, "is divisible by", divi

OpenStudy (anonymous):

Here it is again: testmax = int(raw_input("Enter the maximum number to test: ")) while (testmax > 1): if (testmax % 2) == 1: #this gets me all odd numbers under testmax divisor = testmax - 1 #this gets me all integers under testmax as divisors while (divisor > 0): if (testmax % divisor) == 0: #if this is true then testmax is divisible by divisor print testmax, "is divisible by", divisor divisor = divisor - 1 testmax = testmax - 1

OpenStudy (anonymous):

What did you want it to do? It appears as though it will print out all the factors of all the non-prime numbers less than the number you entered.

OpenStudy (anonymous):

Actually I think I may have figured out how to do this differently. I was trying to solve the first problem in problem set #1. A couple of things I didn't realize: 1) there are handouts for each lecture, and 2) I think watching lecture #3 helped a great deal with the first problem set. I was trying to write this without viewing the 3rd lecture. Once I give it another go, I'll post my code again (using dpaste.com this time). Thanks for your help.

OpenStudy (anonymous):

Just to summarize, I'm trying to generate a script that will print out the 1000th prime number (starting from 1). So far, I've got one piece of this. I've created a piece of code that will test to see if a number is prime or not. I'm not sure how I go about creating a list of numbers to now cycle through this piece of code. Can someone help? My code is here: http://dpaste.com/491394/ Thanks in advance :)

OpenStudy (anonymous):

This is very good. Now, write a second piece of code (independently from this one), which simply prints the numbers from 2 to 100.

OpenStudy (anonymous):

ok...how's this? http://dpaste.com/493317/

OpenStudy (anonymous):

That'll do. Now take the code you had before and combine it with this one, where you're testing each number as you loop through the numbers from 2 to 100. (testnum in the first code is count in the second)

OpenStudy (anonymous):

Thanks Polpak for your help. I can't tell you how much I appreciate it. How does this look? http://dpaste.com/499735/

OpenStudy (anonymous):

Wait a sec, I may have misunderstood your last post Polpak. You wanted me to test each number from 2-100 to see if it was prime. The code I just submitted doesn't do that. My code allows the user to choose what number to test, then runs through a for loop to test it. Let me have another go. Sorry for misunderstanding.

OpenStudy (anonymous):

Ok, I would think this would work, but apparently it doesn't. Any ideas? http://dpaste.com/500015/

OpenStudy (anonymous):

Haha!!! Figured it out: http://dpaste.com/500041/ The variables have to be declared inside the for loop :)

OpenStudy (anonymous):

And lastly my solution to the first problem on problem set #1: http://dpaste.com/500276/ Thanks again Polpak.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!