Prime number

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 35
Reputation: Darkangelchick is an unknown quantity at this point 
Solved Threads: 1
Darkangelchick Darkangelchick is offline Offline
Light Poster

Prime number

 
0
  #1
Dec 4th, 2008
Hi im writing a program that returns whether an inputted number is prime or not, but Im kind of stuck...

  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
H@nn@K*:)
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Prime number

 
0
  #2
Dec 4th, 2008
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.
  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:
  1. if is_prime(primeNum):
  2. print "The number is prime."
  3. else:
  4. print "Nope."
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,031
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 290
woooee woooee is offline Offline
Veteran Poster

Re: Prime number

 
0
  #3
Dec 5th, 2008
Your last 2 lines would be modified thusly
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Prime number

 
0
  #4
Dec 5th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 1
Reputation: Progressivism is an unknown quantity at this point 
Solved Threads: 0
Progressivism Progressivism is offline Offline
Newbie Poster

Re: Prime number

 
-1
  #5
Jul 18th, 2009
primeNum = int(raw_input("Enter number here:"))
print listfactors(primeNum)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC