anonymous
  • anonymous
Hello guys. Starting on problem set 1, problem 1. I figured it would help me to write a function for prime numbers before I moved on to the next part of the problem. However, thats not working very well. http://dpaste.com/547820/ The issue is that a prime number, say 5, would meet the conditions i ask in the script and it would print at the end, but so would the number 4, which is not a prime number.
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
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.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Owlfred
  • Owlfred
Hoot! You just asked your first question! Hang tight while I find people to answer it for you. You can thank people who give you good answers by clicking the 'Good Answer' button on the right!
anonymous
  • anonymous
All numbers will fit the first condition, divide anything by 1 and you are left with the same number. Divide anything by itself and there will be no remainder. Therefore, it will print x if y can't be divided into it, or it will do nothing otherwise. Your example prints 4 because you can't divide 3 into 4 (evenly), even though you can divide 2 into 4 (you are not checking for that).
anonymous
  • anonymous
I don't know if it helps, but I just finished this assignment, and the first thing I did in my function was check to see if any test number divided by either 2 or 3 had a remainder. That eliminates any even numbers and multiples of 3.

Looking for something else?

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

More answers

anonymous
  • anonymous
Here is what I came up with and it appears to work: def isPrime(cand): prime = None if (cand==1) or (cand%2==0 and cand!=2): prime = False else : for divisor in range(3,(cand/3+1),2): if cand%divisor==0: prime = False if prime == False: return False else: return True Calling isPrime(cand) will return either True or False eg. >>> isPrime(9) False >>> isPrime(7919) True Hope this helps :)
1 Attachment

Looking for something else?

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