473,597 Members | 2,177 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:docume nts/python programs/Chapter 4/prime", line 10
for i in range(2,math.sq rt(num)):
DeprecationWarn ing: 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 2173
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:docume nts/python programs/Chapter 4/prime", line 10
for i in range(2,math.sq rt(num)):
DeprecationWarn ing: 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: DeprecationWarn ing: 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:docume nts/python programs/Chapter 4/prime", line 10
for i in range(2,math.sq rt(num)):
DeprecationWarn ing: 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 "DeprecationWar ning".
Jun 1 '07 #5
ghostdog74
511 Recognized Expert Contributor
Yeah. By "that" I meant "DeprecationWar ning".
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 another way to bind only to for example? -- Jon Angel HHS
0
1373
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 into a number field? Or, another scenario, if a textbox is left intentionally blank that is required to contain a number?
5
2116
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. how can i set up from "//localhost:numbers/" to just "//localhost/" so that other people could see the page not the error message. .....
19
3571
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 right most digit with this: right_number = number % 10; /* print the number */
1
1715
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... so my program takes this average and gives me 2.0013 in the log file. After it uses this shorter version for other calculations.instead of my original 2.0013005 (It cuts it and well that should NOT be a problem, but because of the arithmetics I use....
3
6288
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 works like this: First, the user enters a number to test (to find the two prime numbers that make up that number). Next the program finds the numbers, outputs them, then quits. What I want to do is create a prompt after the number has been processed...
2
1726
by: thomas | last post by:
#include<iostream> #include<vector> #include<map> #include<set> #include<iterator> #include<string> #include<algorithm> using namespace std;
15
3406
by: John A Grandy | last post by:
Does .NET provide a "factors of" method ? I need to determine the factors of an integer.
0
8272
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5428
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2403
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 we have to send another system
0
1233
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.