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

Need help with Primes from PSET1: I don't know how to generate the primes from a tuple of odd numbers. Am I doing this right??? Here is my code so far: #Generate odd numbers up to 8000 and put into tuple odds = [] for ans in range(1,8000): if (ans/2)*2 != ans: odds = odds + [ans,] #From generated tuple, generate list of primes number = 1 for prime in odds: if number == 1001: break #Divide prime by all numbers less than prime

OpenStudy (anonymous):

Not sure what to do next...

OpenStudy (anonymous):

I'm working on this problem too . . . I didn't know where to start so I wrote a little function to test is any number is a prime. def isPrime(number): // a prime is a number that is only divisible by itself and 1 // start by testing 2, if that succeeds increment count = 2 while(count < number): if number % count == 0: return False else: count += 1 // if our while loop exits, it is a prime return True

OpenStudy (anonymous):

At what point are these exercises supposed to be done? Which lecture do they match up with?

OpenStudy (anonymous):

The calendar gives the correlation between the assignments, quizzes, and videos.

OpenStudy (anonymous):

Charleyramm: That's a very good way to break down the task into cohesive components. Now you can easily iterate the natural numbers and list out the first 1000 primes.

OpenStudy (anonymous):

@ericc You seem to be on the right track You generated a tuple of odd numbers now you might want to see if each number is divisible by any other number/ if it is not then it is a prime number.

OpenStudy (anonymous):

BTW, where you are creating your tuple of odd numbers it might be more straightforward to say that odd numbers have a remainder of 1 divided by 2, so ans % 2 == 1 But even more straightforward, you can give range an additional argument to step by, so for example range(1,100, 2) will be the tuple (1, 3, 5, ... 99), i.e. all numbers <100 starting at 1, counting by 2, i.e. odd numbers < 100.

OpenStudy (anonymous):

I'm in the exact same spot and every time I try to test the value of the items in the tuple against other numbers for a remainder I get an error, no matter how many different ways I try it.

OpenStudy (anonymous):

MelissaB, feel free to start a new question, and link your code (and the error) from a pastebin site like dpaste.com

OpenStudy (anonymous):

@rwmuller - Not sure how to divide a tuple without getting an error @zifmia - Thanks for the tip. I'm trying to work my way through the lectures and assignments, one at a time -- maybe I'll learn that in later lectures? @melissaB - YES! I'm currently trying to turn the tuple into an integer...I'll post my code if it works.

OpenStudy (anonymous):

tuples are not integers. An integer is a whole number, a tuple is an immutable, ordered collection of objects. There's not really a way to turn one into the other. Can either of you clarify what the trouble is?

OpenStudy (anonymous):

@ericc What makes an integer a prime number? How could you test to see if a number meets that condition. You may also want a list to store numbers that are primes

OpenStudy (anonymous):

@rwmuller - A prime number is a number that can only be divided by itself and 1. To test if a number(n) > 3 is prime, you can divide n by the range (2,n-1) and check if there is a remainder == 0 (If yes, then not prime and skip. If no, prime and add to possibles list). @polpak - I have generated all of the odd numbers <8000 and placed them into a tuple. I now want to divide each odd number(n) by the range(2, n-1). Here is where I run into difficulties -- in order to do that, I must change each odd number(in the tuple) into an integer. You're saying I cannot do this? Maybe using a tuple is not necessary?

OpenStudy (anonymous):

@ericcc - All the odd numbers in your tuple are already integers. (assuming they're actual numbers and not string representations of numbers) I think you're making this task more difficult than it's intended to be. You don't need to generate a list of the odd numbers, you can simply write a while loop that iterates through them one at a time. Have a counter that keeps track of how many primes you've found, when that counter is >= 1000, you've found 1000 primes, so you stop looping. Something like.... print 2 numPrimesFound = 1 #we're taking 2 as a given. current_number = 3 while numPrimesFound < 1000: #test current number and increment numPrimesFound if it's prime #increment current number by 2

OpenStudy (anonymous):

@ericcc I did not use a tuple. As poplak suggests I used a list that I seeded with the first prime I2) then I appended primes as I found them. You have suggested a method of generating a list of target numbers Poplak suggests there are others. From your definition of a prime you can create a test. I iterated through my list of primes. If I didn''t find a remainder then I added it to the list and went on to the next test integer i

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!