A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
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
anonymous
 5 years ago
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

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Not sure what to do next...

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I'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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0At what point are these exercises supposed to be done? Which lecture do they match up with?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0The calendar gives the correlation between the assignments, quizzes, and videos.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Charleyramm: 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0@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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0BTW, 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I'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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0MelissaB, feel free to start a new question, and link your code (and the error) from a pastebin site like dpaste.com

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0@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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0tuples 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?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0@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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0@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,n1) 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, n1). 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?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0@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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0@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
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.