•
•
•
•
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
![]() |
•
•
Join Date: Jan 2008
Posts: 17
Reputation:
Rep Power: 1
Solved Threads: 0
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!
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()•
•
Join Date: Oct 2007
Posts: 41
Reputation:
Rep Power: 1
Solved Threads: 5
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.
I wouldn't define a function in a function. Try classes.
And its good syntax to define a function before you use it.
python Syntax (Toggle Plain Text)
import math #class MyMath: def isEvenAndPrime(n): print "="*52,"\nERROR:",n,"is already prime." 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 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 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) n = input('Enter number to test: ') #MyMath.calcPrime(squareIt, n) etc squareIt = 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
"And da wind cry moron." ~ Pearls Before Swine
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- program help (C++)
Other Threads in the Python Forum
- Previous Thread: How to sort word (from file) frequancy in decrease order? I need help
- Next Thread: floating numbers


Linear Mode