i need help with problem 1 from problem set 1
here's my attempt at the problem : http://dpaste.com/501964/
i figured out there's something wrong with the loop but i can't figure out how to fix it,
in fact if i try this : http://dpaste.com/501971/
can someone give me an insight on how should i approach the code ?
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 don't have time to try and help you sorry i have to go to bed but here's is how i did it...
My approach is different than others because I put my numbers into a list but I only did this because it made more sense to me. There are other ways it could be done.
well.. one way ...
for every candidate you want to loop through divisors and test if candidate mod divisor equals zero.
- if it is divisible you don't want that candidate so don't do anything with it - you can use the break statement to break out of that loop and go to the next candidate.
- for a prime the program will loop through all possible divisors (the mod test will fail for all) and that loop will just stop cause it reached the end- you have to be able to test for this condition
both of the scenarios above put you at the same place in your code. when you get there what will your divisor equal ?
i only looked at your first posted code -
- I noticed that you limited the range of divisors to "sqrt (candidate)" did that work? shouldn't it be math.sqrt?
- you are trying to increment candidate in too many places, your overall while loop will continue to run till you find the 1000th prime - only increment candidate right before you are ready to loop and try a new one
hope that made sense and helos
epan: in your code, your inner most while loop is doing far too much work It should only be looking for divisors, not incrementing the candidate. It sometimes helps to write things in a more general sense..
current number is 3
number of primes found is 1
while number of primes found is less then the number needed:
divisor is 2
while divisor is a possible factor of current number:
if divisor divides the current number:
stop looking for divisors
divisor is the next possible divisor
if divisor does not divide the current number:
the current number is prime
increment the number of primes found
current number is the next possible number
It can also help to make a flow chart to model your process.
Not the answer you are looking for? Search for more explanations.
here is my code for the first problem:
def prime(x) : #define a function to determine if a number is prime or not
for t in range(2,x-1):
n=int(raw_input('What prime number do you want?'))
the second problem is really easy if u understand the first. here is the code:
from math import *
def prime(x) :
for t in range(2,x-1):
print 'The sum of the logarithms of all prime numbers from 2 to' ,n, ' is',s