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 :)
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.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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!
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).
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.
Not the answer you are looking for? Search for more explanations.
Here is what I came up with and it appears to work:
prime = None
if (cand==1) or (cand%2==0 and cand!=2):
prime = False
for divisor in range(3,(cand/3+1),2):
prime = False
if prime == False:
Calling isPrime(cand) will return either True or False
Hope this helps :)