A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Hi guys, i have completed the firstproblem in ps 1..got the output ..i want someone to take a look and give any suggestions ..i think my coding was too naive compared to other programs here.... thanks in advance...

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

    primecount=0 testnumber=3 divisor=2 while(testnumber%2!=0): while(divisor<testnumber): if testnumber%divisor!=0: divisor=divisor+1 else: break else: primecount=primecount+1 if(primecount==999): print testnumber break testnumber=testnumber+2 divisor=2

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

    I think it looks pretty good. The only thing is it looks as though it will test for a divisor up to one less than the testnumber. Ex. testnumber is 999, divisor will test to 998, which you do not need to do. You can stop the divisor much earlier. This would only make a difference the higher your testnumber becomes.

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

    That will work, though I dislike the use of the else clause on loops as I find them very difficult to read. I much prefer the use of boolean variables as controls. primecount=1 # we're starting at 3, so we add 1 to our count for 2 (the first prime) testnumber=3 while primecount < 1000: # find the 1000th prime divisor=2 divisorFound = False while (divisor<testnumber) and (not divisorFound): if testnumber%divisor==0: divisorFound = True divisor = divisor + 1 if divisorFound: primecount = primecount + 1 testnumber=testnumber+2

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

    blah.. this interface is terrible at formatting code. that divisor = divisor + 1 line should be unindented to the same level as the if not in the if's block.

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

    thankyou..the interface of this website was too bad.....your program was neat

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

    i think you are also using all the divisors less than the number ..its not needed and it takes more time ....using squareroot saves more compilation time...

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