anonymous
  • anonymous
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/ it works. can someone give me an insight on how should i approach the code ? thank you
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 :)
SOLVED
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.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
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... http://dpaste.com/502108/ 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.
anonymous
  • anonymous
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
anonymous
  • anonymous
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 else 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.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
here is my code for the first problem: def prime(x) : #define a function to determine if a number is prime or not count=0 for t in range(2,x-1): if x%t==0: count=count+1 if count==0: return 1 else : return 0 i=1 x=3 n=int(raw_input('What prime number do you want?')) if n==1: print 2 else : while i0: i=i+1 x=x+2 print x-2
anonymous
  • anonymous
the second problem is really easy if u understand the first. here is the code: from math import * def prime(x) : count=0 for t in range(2,x-1): if x%t==0: count=count+1 if count==0: return 1 else : return 0 s=log(2) x=3 n=float(raw_input('n=')) while x<=n: if prime(x)>0: s=s+log(x) x=x+2 print 'The sum of the logarithms of all prime numbers from 2 to' ,n, ' is',s print n print s/n

Looking for something else?

Not the answer you are looking for? Search for more explanations.