I know there has been lot of discussion on Prime numbers program but I have very specific question. Here is my program: -
# Find the prime numbers
-
# This is a user input program that lets you decide if a number is a prime or not
-
-
import math
-
-
num = int(raw_input("Please enter a number: "))
-
#num = int(math.sqrt(num))
-
-
-
for i in range(2,math.sqrt(num)):
-
#for i in range(2,num/2):
-
prime = num%i
-
if prime == 0.0:
-
print "It is not a prime"
-
break
-
else:
-
print "It is a prime"
-
break
-
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.
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: -
# Find the prime numbers
-
# This is a user input program that lets you decide if a number is a prime or not
-
-
import math
-
-
num = int(raw_input("Please enter a number: "))
-
#num = int(math.sqrt(num))
-
-
-
for i in range(2,math.sqrt(num)):
-
#for i in range(2,num/2):
-
prime = num%i
-
if prime == 0.0:
-
print "It is not a prime"
-
break
-
else:
-
print "It is a prime"
-
break
-
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.
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: -
# Find the prime numbers
-
# This is a user input program that lets you decide if a number is a prime or not
-
-
import math
-
-
num = int(raw_input("Please enter a number: "))
-
#num = int(math.sqrt(num))
-
-
-
for i in range(2,math.sqrt(num)):
-
#for i in range(2,num/2):
-
prime = num%i
-
if prime == 0.0:
-
print "It is not a prime"
-
break
-
else:
-
print "It is a prime"
-
break
-
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: - import math
-
-
def is_prime():
-
num = int(raw_input("Please enter a number: "))
-
for i in range(2,int(math.ceil(num**0.5))):
-
if num % i == 0:
-
print "%s is not a prime" % (num)
-
return False
-
print "%s is a prime" % (num)
-
return True
-
-
is_prime()
Here's another version: - def is_prime(n):
-
count = 2
-
while count < n**0.5:
-
if n % count == 0:
-
print '%d is a product of %d and %d' % (n, count, n/count)
-
return False
-
count += 1
-
print n, 'is a prime number'
-
return True
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".
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".
Yeah. By "that" I meant "DeprecationWar ning".
oh i see..lol..:):): )
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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?
|
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.
.....
|
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 */
|
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....
| |
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...
|
by: thomas |
last post by:
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<iterator>
#include<string>
#include<algorithm>
using namespace std;
|
by: John A Grandy |
last post by:
Does .NET provide a "factors of" method ?
I need to determine the factors of an integer.
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |