| | |
Prime number
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 35
Reputation:
Solved Threads: 1
Hi im writing a program that returns whether an inputted number is prime or not, but Im kind of stuck...
Now thats what I have so far but its not working and I was wondering if anyone could help point out my mistake and help me fix the last 2 lines.
Thank you
Python Syntax (Toggle Plain Text)
import math def listfactors(n): # defining the function l = [] for i in range (1, int(math.sqrt(n))+1): if float(n)/float(i) == int(float(n)/float(i)): l.append(i) l.append(n/i) return l primeNum = int(raw_input("Enter number here:")) if listfactors(primeNum) = print "prime"
Now thats what I have so far but its not working and I was wondering if anyone could help point out my mistake and help me fix the last 2 lines.
Thank you
H@nn@K*:)
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
If you want to determine if a number is prime or not, you should return True (if prime) or False (if not prime). Use the modulo operator to determine if the entered number is divisible by loop variable i.
If the statement evaluates True, return False. If the loop continues until it finishes, return True because the number must be prime.
The name of your function implies you have a different purpose. A better description would be is_prime:
Python Syntax (Toggle Plain Text)
if not n%i:
The name of your function implies you have a different purpose. A better description would be is_prime:
Python Syntax (Toggle Plain Text)
if is_prime(primeNum): print "The number is prime." else: print "Nope."
•
•
Join Date: Dec 2006
Posts: 1,031
Reputation:
Solved Threads: 290
Your last 2 lines would be modified thusly Also, a second for using modulo or divmod. Your function compares integers and floats and you will be comparing numbers like int=1, and float=0.99999 because of the nature of using floats which will always test false. Hopefully you are only testing integers for "primeness" and so should work with integers only. Finally, it is bad form to use "i", "l", or "o" as single letter variables, as they look too much like numbers.
Python Syntax (Toggle Plain Text)
primeNum = int(raw_input("Enter number here:")) prime_list = listfactors(primeNum) ## your function returns a list for prime in prime_list: print "prime =", prime
Last edited by woooee; Dec 5th, 2008 at 12:03 am.
I too suggest the use of % to check for zero remainder, avoiding floating point in this problem.
A nit on operators: when you want integer-style division, discarding any remainder, the proper operator is
A nit on operators: when you want integer-style division, discarding any remainder, the proper operator is
// not / . As in: l.append(n//i) . In this particular case you already know the results are exact so no harm done, but it's good programming practice since it makes your intentions clear. ![]() |
Similar Threads
- prime number question (C)
- C++ prime number program help (C++)
- Average of prime number between 1 & 100 (C#)
- Print Max Prime Number??? (C++)
Other Threads in the Python Forum
- Previous Thread: File Writing Delay?
- Next Thread: How to get WSDL from web services created using SOAPpy
| Thread Tools | Search this Thread |
accessdenied apache application argv array beginner book builtin change chmod converter countpasswordentry curved dan08 dictionary dynamic edit enter examples file filename float format function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






