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

# the same old "prime numbers" question

 P: 2 I know there has been lot of discussion on Prime numbers program but I have very specific question. Here is my program: Expand|Select|Wrap|Line Numbers # Find the prime numbers # This is a user input program that lets you decide if a number is a prime or not   import math   num = int(raw_input("Please enter a number: ")) #num = int(math.sqrt(num))     for i in range(2,math.sqrt(num)): #for i in range(2,num/2):     prime = num%i     if prime == 0.0:         print "It is not a prime"         break     else:         print "It is a prime"         break   1) I get an error saying the following: Warning (from warnings module): File "/Users/trillian/Documents/programs:documents/python programs/Chapter 4/prime", line 10 for i in range(2,math.sqrt(num)): DeprecationWarning: integer argument expected, got float Now does range takes ONLY integers or can it take float as well? 2) If you test my program it works well, but it gives "9" as a prime. :-( 3) How do I loop such that I do not need to use "break" statement and still it tells me if a number is a prime or not? Thanks a lot. Jun 1 '07 #1
5 Replies

 Expert 5K+ P: 6,596 I know there has been lot of discussion on Prime numbers program but I have very specific question. Here is my program: Expand|Select|Wrap|Line Numbers # Find the prime numbers # This is a user input program that lets you decide if a number is a prime or not   import math   num = int(raw_input("Please enter a number: ")) #num = int(math.sqrt(num))     for i in range(2,math.sqrt(num)): #for i in range(2,num/2):     prime = num%i     if prime == 0.0:         print "It is not a prime"         break     else:         print "It is a prime"         break   1) I get an error saying the following: Warning (from warnings module): File "/Users/trillian/Documents/programs:documents/python programs/Chapter 4/prime", line 10 for i in range(2,math.sqrt(num)): DeprecationWarning: integer argument expected, got float Now does range takes ONLY integers or can it take float as well? 2) If you test my program it works well, but it gives "9" as a prime. :-( 3) How do I loop such that I do not need to use "break" statement and still it tells me if a number is a prime or not? Thanks a lot. >>> i = 2.5 >>> for j in range(i): ... print j ... :1: DeprecationWarning: integer argument expected, got float 0 1 >>> It's funny that the docs don't mention anything about this. For floating point ranges you'll need to use the numpy (I think - i don't have my book handy) module. Clearly, your alorithm needs to know about more than just the integer parts of the range. a google search for "python float range" turned up this slew of stuff. Jun 1 '07 #2

 Expert Mod 2.5K+ P: 2,851 I know there has been lot of discussion on Prime numbers program but I have very specific question. Here is my program: Expand|Select|Wrap|Line Numbers # Find the prime numbers # This is a user input program that lets you decide if a number is a prime or not   import math   num = int(raw_input("Please enter a number: ")) #num = int(math.sqrt(num))     for i in range(2,math.sqrt(num)): #for i in range(2,num/2):     prime = num%i     if prime == 0.0:         print "It is not a prime"         break     else:         print "It is a prime"         break   1) I get an error saying the following: Warning (from warnings module): File "/Users/trillian/Documents/programs:documents/python programs/Chapter 4/prime", line 10 for i in range(2,math.sqrt(num)): DeprecationWarning: integer argument expected, got float Now does range takes ONLY integers or can it take float as well? 2) If you test my program it works well, but it gives "9" as a prime. :-( 3) How do I loop such that I do not need to use "break" statement and still it tells me if a number is a prime or not? Thanks a lot. 'range()' expects an integer. I modified your code a bit: Expand|Select|Wrap|Line Numbers import math   def is_prime():     num = int(raw_input("Please enter a number: "))     for i in range(2,int(math.ceil(num**0.5))):         if num % i == 0:             print "%s is not a prime" % (num)             return False     print "%s is a prime" % (num)     return True   is_prime() Here's another version: Expand|Select|Wrap|Line Numbers def is_prime(n):     count = 2     while count < n**0.5:         if n % count == 0:             print '%d is a product of %d and %d' % (n, count, n/count)             return False         count += 1     print n, 'is a prime number'     return True Jun 1 '07 #3

 Expert 100+ P: 511 It's funny that the docs don't mention anything about this. hi, hmm..i think it does. Here . it says "arguments must be plain integers". Jun 1 '07 #4

 Expert 5K+ P: 6,596 hi, hmm..i think it does. Here . it says "arguments must be plain integers". Yeah. By "that" I meant "DeprecationWarning". Jun 1 '07 #5

 Expert 100+ P: 511 Yeah. By "that" I meant "DeprecationWarning". oh i see..lol..:):):) Jun 2 '07 #6 