User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 401,523 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,353 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 221 | Replies: 1
Reply
Join Date: Jan 2008
Posts: 17
Reputation: sigkill9 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
sigkill9 sigkill9 is offline Offline
Newbie Poster

Help Need help streamlining this code please

  #1  
Mar 15th, 2008
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!

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()
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Posts: 41
Reputation: 1337455 10534 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
1337455 10534 1337455 10534 is offline Offline
Light Poster

Re: Need help streamlining this code please

  #2  
Mar 16th, 2008
I'm not gonna bother changing your approach, just perhaps you code.
I wouldn't define a function in a function. Try classes.
And its good syntax to define a function before you use it.
  1. import math
  2. #class MyMath:
  3. def isEvenAndPrime(n):
  4. print "="*52,"\nERROR:",n,"is already prime."
  5. def primeTest(p):
  6. stop = int(math.ceil(math.sqrt(p/2)))+1
  7. for i in range(3, stop, 2):
  8. #print "testing", i, p, p%i
  9. if p % i == 0:
  10. return False
  11. return True # is a prime
  12. def isPrime(p):
  13. if p == 2:
  14. return True
  15. else:
  16. result = primeTest(p)
  17. return result
  18. return False
  19. def isEven(num):
  20. if n %2 == 0:
  21. return True # is even
  22. else:
  23. print "="*52,"\nERROR:",n,"is not even. Please use only even numbers.\n"
  24. return False
  25. def calcPrime(squareI2t,n):
  26. if n == 2 or n == 3: # couldnt figure out how to handle these small
  27. isEvenAndPrime(n)# primes, so I forced prime detection
  28. elif n == 4 or n == 6:
  29. print "="*52,"\nFound two prime numbers that sum to",n,"!"
  30. print n, "=",n/2,"+",n/2,"\n","="*52
  31. else:
  32. for x in range(2, squareIt + 1):
  33. if n % x == 0:
  34. break
  35. if x == squareIt:
  36. isEvenAndPrime(n)
  37.  
  38. n = input('Enter number to test: ')
  39. #MyMath.calcPrime(squareIt, n) etc
  40. squareIt = math.sqrt(n)
  41. calcPrime(squareIt,n)
  42. if isEven(n):
  43. found = 0
  44. for p in range(3, n/2, 2):
  45. #print "testing", p, n-p
  46. if isPrime(p) and isPrime(n-p):
  47. print "="*52,"\nFound two prime numbers that sum to",n,"!"
  48. print n, '= %d + %d' % (p, n-p),"\n","="*52
  49. found = 1
  50. break
"And da wind cry moron." ~ Pearls Before Swine
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 2:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC