440,033 Members | 1,952 Online
Need help? Post your question and get tips & solutions from a community of 440,033 IT Pros & Developers. It's quick & easy.

# Help, determining if an input value is prime?

 P: 15 I'm doing some reading in a python book and am doing one of the chapter exercises but I cant figure out how to get it to work and was hoping some of you python guru's could help out? Heres description of the problem to be solved: "A positive whole number n > 2 is prime if no number between 2 and the square root of n (inclusive) evenly divides n. Write a program that accepts a value of n as input and determines if the value is prime. If n is not prime, your program should quit as soon as it finds a value that evenly divides n." Heres the code I have so far but it doesnt work very good... Expand|Select|Wrap|Line Numbers def main():       import math       print "This program determines if a number you enter is prime or not.\n"     n = input ("Please enter a number greater than 1: ")       n = abs(n)     i = 2     while i <= math.sqrt(n):         if n % i == 0:             print n, "is not a prime number"         i += 1     print n, "is a prime number!"   main()   It tells me if a number is prime, but if it isnt prime it tells me it isnt, then tells me it is! LOL. I'm still a newbie when it comes to python... I was able to get this far, but as far as "quitting as soon as it finds a value that evenly divides n." I have no clue how to do that... Could someone please help? Thanks a million! Mar 14 '08 #1
6 Replies

 Expert 100+ P: 849 First off, do your importing at the top of the file, outside of functions as a general rule. There are times later you will need to change this, but for now just put them up there. As to your extra message, that's because your code automatically executes the print statement after the while loop. A simple way to fix this is to use a boolean, which can have True or False values. Call it, say, found and default it to False, then if a non-prime is found, set it to True and at the end, if it's still False, print that it's prime. Mar 14 '08 #2

 P: 15 Okay, I tried to do what you suggested. Here is the updated code and the output, but its still doing something strange... New code: Expand|Select|Wrap|Line Numbers import math   def main():       print "This program determines if a number you enter is prime or not.\n"     n = input ("Please enter a number greater than 1: ")     found = False     n = abs(n)     i = 2     while i <= math.sqrt(n):         if n % i == 0:             found = False             print n, "is not a prime number"         else:             found = True         i += 1       if found == True:         print n, "is a prime number!"   main() New result, prime number: Please enter a number greater than 1: 37 37 is a prime number! New result, not prime: Please enter a number greater than 1: 9 9 is not a prime number New result, large number: Please enter a number greater than 1: 78999 78999 is not a prime number 78999 is not a prime number 78999 is not a prime number 78999 is a prime number! What am I doing wrong? Mar 14 '08 #3

 Expert 100+ P: 849 There's a couple issues here: First, don't print before the loop is done, since if more than one divisor exists, it'll print multiple times. Second, the only time you need to change found is if you find a divisor. Mar 14 '08 #4

 P: 16 Also you really need to look into breaking out of the loop when a divisor is found. Maybe there is a simple statement to break out of a while loop ? When approaching a problem like this it helps to work the logic out in English (sometimes called pseudo code) first so for the loop part: Expand|Select|Wrap|Line Numbers while divisor is less than or equal to the square root of number:         test if number mod divisor equals zero:               yes so set found to true and                 exit loop              else no so increment divisor                 continue loop   is found set to true ?     yes so print number " is not a prime"     else print numer "is a prime"   Hopefully you can follow the logic of this (I'm not following any standards here). It should now be simple to translate this to python. Check the problem decription, what's the smallest number you should be prompting for ? Mar 14 '08 #5

 P: 56 well, this would need two things: 1) a divisable list 2) the "check-if-it's-prime" part the first part would require you to fin all the factors of the number Expand|Select|Wrap|Line Numbers def divisable(x):    y=x    #this is here so you don't edit the real value      list=[]    #this will hold all our factors      number = 1    #this is the number we'll divide x by    while number>> Tell me your number: 5 5 is prime.   Tell me your number: 12 12 is NOT prime.   BTW this is not the most effective way, but it works! Mar 14 '08 #6

 Expert Mod 2.5K+ P: 2,851 well, this would need two things: 1) a divisable list 2) the "check-if-it's-prime" part the first part would require you to fin all the factors of the number Expand|Select|Wrap|Line Numbers def divisable(x):    y=x    #this is here so you don't edit the real value      list=[]    #this will hold all our factors      number = 1    #this is the number we'll divide x by    while number>> proper_divisors(39438620) [1, 2, 4, 5, 10, 13, 20, 26, 52, 65, 130, 260, 151687, 303374, 606748, 758435, 1516870, 1971931, 3033740, 3943862, 7887724, 9859655, 19719310] >>>  If all you want to do is check if a number is prime, you can do the same as above without building a list. Expand|Select|Wrap|Line Numbers def is_prime(n):     if n < 2:         return False     elif n == 2:         return True     elif not n%2:         return False     for i in xrange(3, int(n**0.5)+1, 2):         if not n%i:             return False     return True Then all you need to do is check for True or False. Expand|Select|Wrap|Line Numbers def main():     print "This program determines if a number you enter is prime or not.\n"     n = input ("Please enter a number greater than 1: ")     if is_prime(n):         print '%d is a prime number.' % n     else:         print '%d is not a prime number.' % n   main() Mar 14 '08 #7