473,749 Members | 2,486 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 2181
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
1377
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
2125
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
3608
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
1721
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
6298
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
1736
by: thomas | last post by:
#include<iostream> #include<vector> #include<map> #include<set> #include<iterator> #include<string> #include<algorithm> using namespace std;
15
3414
by: John A Grandy | last post by:
Does .NET provide a "factors of" method ? I need to determine the factors of an integer.
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
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
9388
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...
0
9254
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
8256
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
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
4608
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
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
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

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.