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
if sum(answers) != 0:
counter = counter + 1
answers =  # reset answers list to 0 elements
prime = prime + 2 # next odd number (after 2, all primes are odd
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!
I 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.
Last post is your biggest problem. As for why you are getting a memory error, try running your code and then hit control-C to break out of it as fast as you can. Then
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.
Thanks 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.
Not the answer you are looking for? Search for more explanations.
i 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.