Created
March 4, 2014 18:25
-
-
Save myrkvi/9352522 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isPrime(n): | |
if n <= 0: | |
return False | |
elif n == 1: | |
return False | |
elif n == 2: | |
return True | |
else: | |
for x in range(2, (n // 2) + 1 ): | |
if (n % x) == 0: | |
return False | |
return True | |
def primes(n): | |
result = [] | |
for x in range(0, n +1): | |
if isPrime(x): | |
result.append(x) | |
return result | |
def numberOfPrimes(n): | |
primesList = primes(n) | |
numOfPrimes = 0 | |
for x in primesList: | |
numOfPrimes += 1 | |
return numOfPrimes | |
def isTwinPrime(n): | |
if not isPrime(n): | |
return None | |
elif n == 2: | |
return False | |
else: | |
if isPrime( n + 2 ): | |
return True | |
elif isPrime( n - 2): | |
return True | |
else: | |
return False | |
def twinPrimes(n): | |
twinList = [] | |
primesList = primes(n) | |
for x in primesList: | |
if isTwinPrime(x): | |
twinList.append(x) | |
return twinList | |
def numberOfTwinPrimes(n): | |
twinPrimesList = twinPrimes(n) | |
numOfTwinPrimes = 0 | |
for x in twinPrimesList: | |
numOfTwinPrimes += 1 | |
return numOfTwinPrimes | |
def printIsPrime(n): | |
result = isPrime(n) | |
if result == True: | |
print("%i:\tPRIME" % n) | |
else: | |
print("%i:\tCOMPOSITE" % n) | |
print( numberOfTwinPrimes(1) ) | |
print( numberOfTwinPrimes(9) ) | |
print( numberOfTwinPrimes(13) ) | |
print( numberOfTwinPrimes(23) ) | |
print( numberOfTwinPrimes(100) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment