hey peeps, i'm stuck on ps1. how in the *)$# do you test for a prime number?
So, generally speaking, since prime numbers are numbers that are not divisible by anything but 1 and itself, you will have to check, for every number, whether or not there are numbers smaller than it that divide it with 0 remainder. There's an arithmetic 'remainder' operator that is really useful for this in python - % - which you will definitely want to use. It can be used like this remainder = odd_int % divisor You'll have to set up some initial global variables and use these to count through all numbers, checking each to see if there are numbers below it that divide it. Using 'while' loops should do the trick, but hit us up after you've taken a stab at it. You can paste code in http://codepad.org/ for us to look at what you've done so far (make sure you click the 'python' language button). Here's a suggested list of global variables to get started. You can definitely rename them or change their initial values if you want, but hopefully their names give you an idea of what you'll need: odd_int = 3 divisors_of_odd_int = 3 remainder = 1 prime_counter = 2 prime_num = 0
ok, i took a stab at it. this is about where I get stuck every time. I see the prime numbers but i don't know how to isolate them. http://codepad.org/JzFYvClY
Ok, good first attempt. Try again with the following comments in mind: * You are increasing prime_count regardless of whether or not odd_int is prime. You only want to be increasing it if odd int is a prime number. * My code originally did not have a for loop (it's great that you are using one though :) - that said, if you stick with the for loop you don't need the variable 'divisors' at all. Just put: for x in range(1, (odd_int+3)/2): (Also think about why I cut back on the range here.) * It's great that you are checking to see if the remainder is 0. So, if you find an x in this range that gives a remainder 0, you know what about odd_int? [Look up the 'break' function in python - it will spit you out of the for loop entirely :).] Then again, if you go through ALL of the x's in the range and you never get remainder == 0, what do you know about odd_int? theyz shuttin' the website down in a sec but this should give you a good start. good luck / keep askin if u need it.
just take a variable say x = 2 and go on checking till x is less than the required number and if a anytime number say n%x == 0 then break the if statement else the number is prime
definitely closer but think about what happens for EACH x in your for loop. it's best to think of examples: say you are going along in your loop and odd_int == 9. Ok, we know 9 is not prime. Now, for each x between 2 and 8, you first set remainder = odd_int % x. Ok, so x == 2 is the first number your loop checks. But, 9 % 2 == 1, so remainder == 1, you skip over the 'if' statement and then add 1 to prime_counter. So, we know 9 is not prime and yet you have added 1 to your prime counter for 9. Ultimately, you are going to need to write some more conditionals (another 'if' statement in this case). I guess you don't really need the prime_num variable, but you'll have to spit out the final prime number somehow. Good progress and keep at it!
i took a different approach and ended up getting it! http://codepad.org/s04Y7sTQ thanks so much for your help, friend!
great job / keep it up!
Join our real-time social learning platform and learn together with your friends!