Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

dmart

On ps1a, and I don't understand why this returns all of the correct primes... but also a bunch of randomly distributed composite numbers. For example, the first that appears is 95. I know this isn't the best or fastest way to solve this problem, but I don't understand where these random numbers are coming from, it seems like it should work. Can anyone offer any advice? http://pastebin.com/wA2AgDfZ

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. Arbiter
    Best Response
    You've already chosen the best response.
    Medals 2

    The reason you're getting random composites is because you aren't exiting your for loop after coming across a number in "primesrange" that divides your "test" number evenly. Instead you are incrementing the "test" number by 2 and then continuing the divisibility checking where the previous number left off. So, what happens in the case of the number 95 is as follows: Your program correctly determines that 89 is prime. It then starts checking 91 for divisibility by 2, 3, 4, ... until it gets to 7. 91 is evenly divisible by 7 so your "test" number gets incremented to 93 and divisibility checking continues. However, instead of starting back at 2, your program will start at 8, 9, ..., and continue until 31. When you start checking 95, there are no numbers in the range 32, ..., 94 that divide it evenly, so your program decides that it is prime. The easiest way to fix this problem would be to add a break statement on the line after "test = test + 2"

    • 2 years ago
    • Attachments:

See more questions >>>

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.