anonymous
  • anonymous
polpak could you explain this part of the code for Pset1? while (divisor>1): if number%divisor == 0: divisor = 0 else: divisor = divisor - 1
MIT 6.00 Intro Computer Science (OCW)
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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
I'm not sure where this code is from, but presuming that divisor is something like number-1 initially. It would seem to me that we are looping over each number less than 'number' and seeing if 'number' is divisible evenly by the divisor. If it is, then the number is not prime and we can stop early. Otherwise we keep going. The only difficulty is that it's impossible to know after the loop has finished whether you traversed every number, or whether you ended early. You may want to have a separate boolean variable to denote whether you end early. For example you could have this assignment before the while loop divisorFound = False then in the branch for 'if number%divisor == 0' simply set divisorFound=True Then you can tell with a simple if whether or not you managed to find a factor.
anonymous
  • anonymous
This code was used earlier in one of the other discussion threads. divisor is equal to number/2 initially.
anonymous
  • anonymous
Fair enough. It would work just as well to choose number-1. It would simply add a little extra work since none of the numbers in the range (number/2, number-1] are actually possible divisors.

Looking for something else?

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

More answers

anonymous
  • anonymous
I guess my question is how does that code show if the number is prime or not. I think i might be onto it now. so the code that says if number%divisor == 0 divisor = 0 ## This is saying that if this happens the divisor being used can be factored from the number thus making it not prime?
anonymous
  • anonymous
Yes, because prime numbers are only divisible by themselves and 1. if (number%divisor == 0) and (number != divisor) and (number != 1) then number is not prime The code sets divisor to 0 just as an easy way to get out of the while loop. It knows that it doesn't need to search further because we've already found for sure that number is not prime.
anonymous
  • anonymous
err the third part of the condition should be that divisor != 1 actually.
anonymous
  • anonymous
ok thanks. I saw an earlier post that i think it was you who recommended writing out what you wanted each task to accomplish. I think that as long as i can see the progression i'll be able to figure some of these out on my own now. Just needed an understanding of how to get there

Looking for something else?

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