A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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 i<n: if prime(x)>0: i=i+1 x=x+2 print x-2

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.