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

ps1 I can't seem to wrap my head around: testnumber%i == 0: when checking for primes in a range. if testnumber%i == 0: then primenumber = false. When i is 2 and testnumber is two, the remainder is zero...2 is prime, so how does this get counted as a true statement? In other words, for i in range (2, testnumber) while my test number is 2...then testnumber divided by i will have a remainder of zero, while i is 2. There is no condition to say: if only one number in the range returns a remainder of zero, then it is prime. Can anyone explain what I am missing about how this function works?

OpenStudy (anonymous):

You should test your numbers beginning from 3. Since you already know 2 is the first prime number in the natural number sequence, this should be no problem. See if my attached codes help address your questions.

OpenStudy (anonymous):

Thank you. I like your solution. My question is more about, how the test for a remainder of zero works in conjunction with i in the given range. I could ask the same question about three, or any other number that is a prime. How does i "move" through the range? If i for a given iteration is some number x, whenever the test number is x the remainder for that number will be zero....When during the process does a number get discarded as composite? What is the condition that says..."well, only one number divided evenly for this instance of i and the test number, so this one is prime...." I'm able to use the functions to solve the actual problem....But, only because I've seen it done. I'm trying to visualize the process that occurs when using the range and remainder zero condition.

OpenStudy (anonymous):

The following is a simple python program that asks user for an input and then checks to see if whether the number is prime. If the number provided is a prime, then it prints out "True" and if the number is not a prime then it prints out "False". I explain the logic below the program. num=int(raw_input("Please provide number to check for primality: ")) def primetest(x): for i in range (x-1, 2, -1): if x%i==0: return(False) return(True) print (primetest(num)) In statement we ask the user to provide an input, we assign the input to a variable called 'num'. We also convert the type of the input to integer since raw_input by default its value is type string. Then we define a function called primetest with one input (i.e. the number we want to test for primality). Inside the function we setup a for loop that ranges over each number less than num. I have chosen to go backwards a decrement the numbers but that's an arbitrary choice. For example, if we want to test the number 5 for primality then it will begin with the number 4 and then go down 2. (4 then 3 then 2). Then inside the for loop we setup the condition that tests to see whether each of the numbers in the range is divisible. If it is divisible by any number less than it then it will immediately step out of the loop and return False. If, on the other hand, the number is divisible then it will stay in the loop and try every number until it reaches 2. If none of them divide into num then the loop gets completed, we step outside the loop, and return the value True as the result of the function. The final statement is invokes the function with the argument typed in by the user. And then it prints the result.

OpenStudy (anonymous):

Now we can answer your question more directly. If the number we want to test for primality is divisible by any number below it (i.e. if condition ==0) then we know it's not a prime number and can immediately step out of the loop. If, on the other hand, the condition is false we need stay in the loop to make sure that we have checked all the numbers. If the loop completes itself having gone through the entire range without having any divisors then we know the number is a prime.

OpenStudy (anonymous):

Huge light bulb, I completely get it! Thank you! Great explanation!

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!