anonymous
  • anonymous
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
MIT 6.00 Intro Computer Science (OCW)
chestercat
  • chestercat
See more answers at brainly.com
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.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions

anonymous
  • anonymous
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"

Looking for something else?

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