By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

Need help streamlining this code

P: 15
Could someone help me streamline this? Like eliminate unnecessary coding and etc?

I got the script to work, although I couldn't figure out how to handle small prime numbers such as 2 and 3. It wouldn't output "this number is prime" (see script below) so I had to force prime detection. Also, 6 = 3 + 3, and 4 = 2 + 2, but it wouldn't output that either, instead it would just return to the python prompt, so I forced those two.

Thanks!

Expand|Select|Wrap|Line Numbers
  1. import math
  2.  
  3. def main():
  4.  
  5.     def calcPrime(squareI2t,n):
  6.         if n == 2 or n == 3: # couldnt figure out how to handle these small
  7.             isEvenAndPrime(n)# primes, so I forced prime detection
  8.         elif n == 4 or n == 6:
  9.             print "="*52,"\nFound two prime numbers that sum to",n,"!"
  10.             print n, "=",n/2,"+",n/2,"\n","="*52
  11.         else:
  12.             for x in range(2, squareIt + 1):
  13.                 if n % x == 0:
  14.                     break
  15.                 if x == squareIt:
  16.                     isEvenAndPrime(n)
  17.     def isEvenAndPrime(n):
  18.         print "="*52,"\nERROR:",n,"is already prime."
  19.     def isPrime(p):
  20.         if p == 2:
  21.             return True
  22.         else:
  23.             result = primeTest(p)
  24.             return result 
  25.             return False
  26.     def isEven(num):
  27.         if n %2 == 0:
  28.             return True # is even
  29.         else:
  30.             print "="*52,"\nERROR:",n,"is not even. Please use only even numbers.\n"
  31.             return False
  32.     def primeTest(p):
  33.         stop = int(math.ceil(math.sqrt(p/2)))+1
  34.         for i in range(3, stop, 2):
  35.             #print "testing", i, p, p%i
  36.             if p % i == 0:
  37.                 return False
  38.         return True # is a prime
  39.  
  40.     n = int(raw_input('Enter number to test: '))
  41.     squareIt = int(math.sqrt(n))
  42.     calcPrime(squareIt,n)
  43.     if isEven(n):
  44.         found = 0
  45.         for p in range(3, n/2, 2):
  46.             #print "testing", p, n-p  
  47.             if isPrime(p) and isPrime(n-p):
  48.                 print "="*52,"\nFound two prime numbers that sum to",n,"!"
  49.                 print n, '= %d + %d' % (p, n-p),"\n","="*52
  50.                 found = 1
  51.                 break
  52.  
  53. if __name__ == '__main__':
  54.     main()
  55.  
Mar 15 '08 #1
Share this Question
Share on Google+
9 Replies


Subsciber123
P: 87
Wow. Before you do anything else, please get rid of
Expand|Select|Wrap|Line Numbers
  1. def main():
line and un-indent everything below it. This may just be my opinion, but it is bad form to put functions inside functions unless absolutely necessary. Only put the code that needs to be called inside the function, not other function declarations.
Mar 15 '08 #2

P: 15
Mmkay, done. Is there anything else that could be streamlined?
Mar 15 '08 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Separate your user interface code from your functions that perform a function (pun intended). Add 1 to n/2 so 6 will return 3,3. Handle special case 4 to return 2,2. Eliminate import math by using operator **. Use string formatting in your print statements. This is an OOP concept: Send an object to an object and receive a returned object.
Expand|Select|Wrap|Line Numbers
  1. def is_prime(n):
  2.     if n < 2:
  3.         return False
  4.     elif n == 2:
  5.         return True
  6.     elif not n%2:
  7.         return False
  8.     for i in xrange(3, int(n**0.5)+1, 2):
  9.         if not n%i:
  10.             return False
  11.     return True
  12.  
  13. def primes_sum(n):
  14.     if n == 4:
  15.         return 2,2
  16.     for p in range(3, n/2+1, 2):
  17.         p1 = n-p
  18.         if is_prime(p) and is_prime(p1):
  19.             return p, p1
  20.     return None
  21.  
  22. if __name__ == '__main__':
  23.     while True:
  24.         n = int(raw_input('Ener number to test: '))
  25.         if n%2:
  26.             print "%d is not even. Please enter only even numbers." % n
  27.         else:
  28.             break
  29.     s = primes_sum(n)
  30.     if s:
  31.         print '%d = %d + %d' % (n,s[0],s[1])
  32.         print "Found two prime numbers that sum to %d" % n
  33.     else:
  34.         print "No primes for number %d" % n
Mar 15 '08 #4

P: 15
Hi and thanks for the help!

How would I incorporate the prime number check and message into this new code?

I had it in my old code. If a prime number was entered it would print "this number is already prime" then print another message "this number is not even..." yadda yadda.

If I could incorporate that into this new code, it will be complete.
Mar 16 '08 #5

P: 15
Hi and thanks for the help!

How would I incorporate the prime number check and message into this new code?

I had it in my old code. If a prime number was entered it would print "this number is already prime" then print another message "this number is not even..." yadda yadda.

If I could incorporate that into this new code, it will be complete.
Mar 16 '08 #6

P: 15
Not sure why, but it posted 2 of my messages, so I edited this one... sorry.
Mar 16 '08 #7

bvdet
Expert Mod 2.5K+
P: 2,851
Hi and thanks for the help!

How would I incorporate the prime number check and message into this new code?

I had it in my old code. If a prime number was entered it would print "this number is already prime" then print another message "this number is not even..." yadda yadda.

If I could incorporate that into this new code, it will be complete.
The only prime number that is even is 2. Just add an elif to test for 2.
Expand|Select|Wrap|Line Numbers
  1. if __name__ == '__main__':
  2.     while True:
  3.         n = int(raw_input('Ener number to test: '))
  4.         if n%2:
  5.             print "%d is not even. Please enter only even numbers." % n
  6.         elif is_prime(n):
  7.             print "%d is a prime number. Please enter a non-prime number." % n
  8.         else:
  9.             break
  10.     s = primes_sum(n)
  11.     if s:
  12.         print '%d = %d + %d' % (n,s[0],s[1])
  13.         print "Found two prime numbers that sum to %d" % n
  14.     else:
  15.         print "No primes for number %d" % n
Mar 16 '08 #8

P: 15
Maybe I wasnt clear enough, sorry.

I need the program to detect when a prime number has been entered, then output a "This number is prime!" message, for ALL prime numbers, not just the one even one lol.
Mar 16 '08 #9

bvdet
Expert Mod 2.5K+
P: 2,851
Maybe I wasnt clear enough, sorry.

I need the program to detect when a prime number has been entered, then output a "This number is prime!" message, for ALL prime numbers, not just the one even one lol.
Take a good look at the code. All you need to do is swap the code following the if statement and the code following the elif statement. What you want is to test for is_prime() first.
Mar 16 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.