443,270 Members | 1,703 Online 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 import math   def main():       def calcPrime(squareI2t,n):         if n == 2 or n == 3: # couldnt figure out how to handle these small             isEvenAndPrime(n)# primes, so I forced prime detection         elif n == 4 or n == 6:             print "="*52,"\nFound two prime numbers that sum to",n,"!"             print n, "=",n/2,"+",n/2,"\n","="*52         else:             for x in range(2, squareIt + 1):                 if n % x == 0:                     break                 if x == squareIt:                     isEvenAndPrime(n)     def isEvenAndPrime(n):         print "="*52,"\nERROR:",n,"is already prime."     def isPrime(p):         if p == 2:             return True         else:             result = primeTest(p)             return result              return False     def isEven(num):         if n %2 == 0:             return True # is even         else:             print "="*52,"\nERROR:",n,"is not even. Please use only even numbers.\n"             return False     def primeTest(p):         stop = int(math.ceil(math.sqrt(p/2)))+1         for i in range(3, stop, 2):             #print "testing", i, p, p%i             if p % i == 0:                 return False         return True # is a prime       n = int(raw_input('Enter number to test: '))     squareIt = int(math.sqrt(n))     calcPrime(squareIt,n)     if isEven(n):         found = 0         for p in range(3, n/2, 2):             #print "testing", p, n-p               if isPrime(p) and isPrime(n-p):                 print "="*52,"\nFound two prime numbers that sum to",n,"!"                 print n, '= %d + %d' % (p, n-p),"\n","="*52                 found = 1                 break   if __name__ == '__main__':     main()   Mar 15 '08 #1
9 Replies

 P: 87 Wow. Before you do anything else, please get rid of Expand|Select|Wrap|Line Numbers 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

 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 def is_prime(n):     if n < 2:         return False     elif n == 2:         return True     elif not n%2:         return False     for i in xrange(3, int(n**0.5)+1, 2):         if not n%i:             return False     return True   def primes_sum(n):     if n == 4:         return 2,2     for p in range(3, n/2+1, 2):         p1 = n-p         if is_prime(p) and is_prime(p1):             return p, p1     return None   if __name__ == '__main__':     while True:         n = int(raw_input('Ener number to test: '))         if n%2:             print "%d is not even. Please enter only even numbers." % n         else:             break     s = primes_sum(n)     if s:         print '%d = %d + %d' % (n,s,s)         print "Found two prime numbers that sum to %d" % n     else:         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

 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 if __name__ == '__main__':     while True:         n = int(raw_input('Ener number to test: '))         if n%2:             print "%d is not even. Please enter only even numbers." % n         elif is_prime(n):             print "%d is a prime number. Please enter a non-prime number." % n         else:             break     s = primes_sum(n)     if s:         print '%d = %d + %d' % (n,s,s)         print "Found two prime numbers that sum to %d" % n     else:         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

 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 