By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
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
  1. # Find the prime numbers
  2. # This is a user input program that lets you decide if a number is a prime or not
  3.  
  4. import math
  5.  
  6. num = int(raw_input("Please enter a number: "))
  7. #num = int(math.sqrt(num))
  8.  
  9.  
  10. for i in range(2,math.sqrt(num)):
  11. #for i in range(2,num/2):
  12.     prime = num%i
  13.     if prime == 0.0:
  14.         print "It is not a prime"
  15.         break
  16.     else:
  17.         print "It is a prime"
  18.         break
  19.  
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
Share this Question
Share on Google+
5 Replies


bartonc
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
  1. # Find the prime numbers
  2. # This is a user input program that lets you decide if a number is a prime or not
  3.  
  4. import math
  5.  
  6. num = int(raw_input("Please enter a number: "))
  7. #num = int(math.sqrt(num))
  8.  
  9.  
  10. for i in range(2,math.sqrt(num)):
  11. #for i in range(2,num/2):
  12.     prime = num%i
  13.     if prime == 0.0:
  14.         print "It is not a prime"
  15.         break
  16.     else:
  17.         print "It is a prime"
  18.         break
  19.  
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
...
<string>: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

bvdet
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
  1. # Find the prime numbers
  2. # This is a user input program that lets you decide if a number is a prime or not
  3.  
  4. import math
  5.  
  6. num = int(raw_input("Please enter a number: "))
  7. #num = int(math.sqrt(num))
  8.  
  9.  
  10. for i in range(2,math.sqrt(num)):
  11. #for i in range(2,num/2):
  12.     prime = num%i
  13.     if prime == 0.0:
  14.         print "It is not a prime"
  15.         break
  16.     else:
  17.         print "It is a prime"
  18.         break
  19.  
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
  1. import math
  2.  
  3. def is_prime():
  4.     num = int(raw_input("Please enter a number: "))
  5.     for i in range(2,int(math.ceil(num**0.5))):
  6.         if num % i == 0:
  7.             print "%s is not a prime" % (num)
  8.             return False
  9.     print "%s is a prime" % (num)
  10.     return True
  11.  
  12. is_prime()
Here's another version:
Expand|Select|Wrap|Line Numbers
  1. def is_prime(n):
  2.     count = 2
  3.     while count < n**0.5:
  4.         if n % count == 0:
  5.             print '%d is a product of %d and %d' % (n, count, n/count)
  6.             return False
  7.         count += 1
  8.     print n, 'is a prime number'
  9.     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

bartonc
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

Post your reply

Sign in to post your reply or Sign up for a free account.