Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 22 Online
OpenStudy (anonymous):

so, ratios. I got my PS1B to work properly, more or less -- but is there a way to have Python calculate a simpler ratio rather than just printing out the sum and n with a colon between them? Here's my code: http://pastebin.com/MEjBhpJ9

OpenStudy (anonymous):

Sure. You can reduce the ratio to a fraction or represent it as a percentage or even divide the larger number by the smaller and give the ratio in the form of 1:x or x:1. Note in your code that using 1 as a prime means that your candidates will never be marked prime. That is to say, any number mod 1 will return a 0 because everything is evenly divisible by 1. Since primes are defined as being greater than 1, you can just toss it. At line 7, you might simply check for less than rather than <= since you're looking for numbers less than the given. Your summing loop later does this correctly anyway, so it only saves you a few processing cycles.

OpenStudy (anonymous):

check out the table in this section of the docs- http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex ratio = sum_it_up / n print ratio

OpenStudy (anonymous):

thanks!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!