A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
can somebody help me with PS1? I keep getting Memory Error.... Thanks!
n = 1000
n = n  1 # not counting 2 (the only even prime)
counter = 1
prime = 3 # 3 is 2nd prime
divisor = [2, ]
answers = []
while counter <= n:
for div in divisor:
ans = prime % div
answers.append(ans)
if sum(answers) != 0:
counter = counter + 1
divisor.append(prime)
answers = [] # reset answers list to 0 elements
prime = prime + 2 # next odd number (after 2, all primes are odd
print prime
anonymous
 5 years ago
can somebody help me with PS1? I keep getting Memory Error.... Thanks! n = 1000 n = n  1 # not counting 2 (the only even prime) counter = 1 prime = 3 # 3 is 2nd prime divisor = [2, ] answers = [] while counter <= n: for div in divisor: ans = prime % div answers.append(ans) if sum(answers) != 0: counter = counter + 1 divisor.append(prime) answers = [] # reset answers list to 0 elements prime = prime + 2 # next odd number (after 2, all primes are odd print prime

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I am not sure why you are using the sum function in your process. It strikes me that if division by any number yields no remainder then the number is not prime. I am not sure that your design catches that.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Last post is your biggest problem. As for why you are getting a memory error, try running your code and then hit controlC to break out of it as fast as you can. Then print divisor You should see something like [2, 3, 3, 3, 3, .... (few hundred more 3's depending on your reflexes), 3, 3] You created an infinite loop by adding on to the end of the list you are looping through. Adding to the list of divisors when you find a prime is fine, just do it outside of the loop.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Thanks rwmuller and zifmia. I rewrote the program several times but didn't see these problems until you guys pointed it out... @rwmuller: I added the sum because I thought that was the way to catch any divisors which are factors of the number divided. @zifmia: I put the append inside the if block because if not it would any prime candidates which aren't actually primes.. Just thought I should talk about the decisions I made when I wrote the faulty program above. These are all conceptual errors on my part, but I still don't really understand why they don't work.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0i know unlike c , python is garbage collected but taking an empty array in this way feels wrong as no memory is allocated for it, for allowing this assignment operation for the array a[], a has to be dynamically allocated in the heap, which does not seem to be the case. if its not dynamically allocated(like strings are immutable in python, default strings in objective c)when adding elements it will create a memory leak, and may be segmentation fault.
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.