943,618 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 982
  • Python RSS
Dec 4th, 2008
0

Prime number

Expand Post »
Hi im writing a program that returns whether an inputted number is prime or not, but Im kind of stuck...

Python Syntax (Toggle Plain Text)
  1. import math
  2. def listfactors(n): # defining the function
  3. l = []
  4. for i in range (1, int(math.sqrt(n))+1):
  5. if float(n)/float(i) == int(float(n)/float(i)):
  6. l.append(i)
  7. l.append(n/i)
  8. return l
  9.  
  10. primeNum = int(raw_input("Enter number here:"))
  11. if listfactors(primeNum) =
  12. 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
Similar Threads
Reputation Points: 8
Solved Threads: 1
Light Poster
Darkangelchick is offline Offline
35 posts
since Aug 2008
Dec 4th, 2008
0

Re: Prime number

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.
Python Syntax (Toggle Plain Text)
  1. if not n%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)
  1. if is_prime(primeNum):
  2. print "The number is prime."
  3. else:
  4. print "Nope."
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Dec 5th, 2008
0

Re: Prime number

Your last 2 lines would be modified thusly
Python Syntax (Toggle Plain Text)
  1. primeNum = int(raw_input("Enter number here:"))
  2. prime_list = listfactors(primeNum) ## your function returns a list
  3. for prime in prime_list:
  4. print "prime =", prime
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.
Last edited by woooee; Dec 5th, 2008 at 12:03 am.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006
Dec 5th, 2008
0

Re: Prime number

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 // 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.
Reputation Points: 94
Solved Threads: 48
Posting Whiz
BearofNH is offline Offline
321 posts
since May 2007
Jul 18th, 2009
-1

Re: Prime number

primeNum = int(raw_input("Enter number here:"))
print listfactors(primeNum)
Reputation Points: 9
Solved Threads: 0
Newbie Poster
Progressivism is offline Offline
1 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: File Writing Delay?
Next Thread in Python Forum Timeline: SetFocus() on canvas not working?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC