473,499 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the same old "prime numbers" question

2 New Member
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 2166
bartonc
6,596 Recognized Expert Expert
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 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
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 Recognized Expert Expert
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 Recognized Expert Contributor
Yeah. By "that" I meant "DeprecationWarning".
oh i see..lol..:):):)
Jun 2 '07 #6

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

Similar topics

1
315
by: Jon Angel | last post by:
Is it possible to create a RowFilter expression for a DataView such that you bind and display only the first 200 rows? I note that the RowViewCache holds each row as , , , etc. Perhaps there's...
0
1370
by: sympatico | last post by:
There are various methods of dealing with error trapping when a user inputs text into a Single, or Double field in a textbox. What have you used as the tightest, cleanest code to prevent text entry...
5
2109
by: KenLee | last post by:
help: how can I change from "//localhost:numbers/" to just "//localhost/"? I can see the web pages using "//localhost:numbers/" instead of ""//localhost/". I use asp.net 2.o. and windows 2000....
19
3526
by: gk245 | last post by:
I have something like this: printf("Enter numbers: "); scanf ("%i", &number); If the user put in a bunch of numbers (like 4568), instead of just one number, i know that you can extract the...
1
1704
by: Daikide | last post by:
I need average for my program. And I'm using it multiple time in my program. I'm doing this in PVSS: So say average float is 2.0013005 (FOR EXAMPLE) but the number in the end can be higher......
3
6271
by: sigkill9 | last post by:
I've been working on this script all weekend and am almost done with it accept for one last detail; implementing a "process another number?" prompt after a user input is processed. The script...
2
1716
by: thomas | last post by:
#include<iostream> #include<vector> #include<map> #include<set> #include<iterator> #include<string> #include<algorithm> using namespace std;
15
3392
by: John A Grandy | last post by:
Does .NET provide a "factors of" method ? I need to determine the factors of an integer.
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.