Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Function 1 - Find the primes
- def findPrimes(limit):
- primes = [2, 3]
- for n in range(4, limit+1):
- isPrime = True
- for prime in primes:
- if n % prime == 0:
- isPrime = False
- break
- if isPrime:
- primes.append(n)
- print("Primes until " + str(limit) + " ----> " + str(primes))
- print()
- return primes
- # FUNCTION 2 - Find the twin primes in a given primes list
- def twinPrimes(limit):
- primes = findPrimes(limit)
- counter = 0
- print("***********************************************")
- for i in range(len(primes)-1):
- if primes[i+1] - primes[i] == 2:
- counter += 1
- print("Twins: " + str(primes[i]) + " and " + str(primes[i+1]))
- print("There are " + str(counter) + " pairs of twin primes from 2 to " + str(limit))
- print("***********************************************")
- print()
- return counter
- # MAIN FUNCTION
- limit1 = 10**3
- counter1 = twinPrimes(limit1)
- limit2 = 10**4
- counter2 = twinPrimes(limit2)
- limit3 = 10**5
- counter3 = twinPrimes(limit3)
- print("From 1 to " + str(limit1) + ": " + str(counter1) + " pairs of twin numbers")
- print("From 1 to " + str(limit2) + ": " + str(counter2) + " pairs of twin numbers")
- print("From 1 to " + str(limit3) + ": " + str(counter3) + " pairs of twin numbers")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement