A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 4 years ago

Problem Set 1, Problem 1. Counting prime numbers question: My program is finding the prime numbers properly, but I can't figure out how to make it count just the prime numbers.

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

    Here's this: http://pastebin.com/duQFNZ6D

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

    It is much simpler then that. start out assuming the number is prime. Next all you have to do is check it the number is divisible by any number up to it's square root. if not it's prime so just break. You can check this with just running a loop to test number % index == 0 and if that test is true then the number is not prime then return false.

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

    Awesome, thanks!

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

    just make sure to start the loop with i = 2 or you will get stuck in an infinite loop.

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

    Right, got it.

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

    You get stuck here is my code. (please try it on your own first) http://pastebin.com/ZZpeULiY

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

    The frustrating problem with jimtuv and some other helpful responses to this question - problem 1 of set 2, is that they use concepts not taught yet in lectures 1 thru 3. Here is my code that was worked out with much time and frustration, but is simple and works with the bare bones concepts of a beginner. The trick is keeping the indentations correct. #generate n (odd numbers 3 to ?) n=3 count = 1 #go to 999 since we skipped 2 while(count<1000): #test n for prime i=2 while(i<n): if n%i==0: i=n if n%i!=0: i=i+1 if i==n: count=count+1 n=n+2 print n-2 ##

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