| | |
Prime number
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,001
Reputation:
Solved Threads: 284
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 |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework ideas import inches input java launcher library line lines linux list lists loop mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






