469,358 Members | 1,596 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

the same old "prime numbers" question

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
5 1835
bartonc
6,596 Expert 4TB
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
2,851 Expert Mod 2GB
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
ghostdog74
511 Expert 256MB
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
6,596 Expert 4TB
hi, hmm..i think it does. Here . it says "arguments must be plain integers".
Yeah. By "that" I meant "DeprecationWarning".
Jun 1 '07 #5
ghostdog74
511 Expert 256MB
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.

Similar topics

1 post views Thread by Jon Angel | last post: by
19 posts views Thread by gk245 | last post: by
2 posts views Thread by thomas | last post: by
15 posts views Thread by John A Grandy | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.