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:
#Divide prime by all numbers less than prime
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Not sure what to do next...
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.
// 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:
count += 1
// if our while loop exits, it is a prime
At what point are these exercises supposed to be done? Which lecture do they match up with?
Not the answer you are looking for? Search for more explanations.
The calendar gives the correlation between the assignments, quizzes, and videos.
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.
@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.
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.
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.
MelissaB, feel free to start a new question, and link your code (and the error) from a pastebin site like dpaste.com
@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.
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?
@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
@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?
@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.
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
@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