Advertisement
makispaiktis

Problem 1 - Goldbach's Theorem

Jul 5th, 2021 (edited)
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. # Function 1 - Find the primes
  2. def findPrimes(limit):
  3.     primes = [2, 3]
  4.     for n in range(4, limit+1):
  5.         isPrime = True
  6.         for prime in primes:
  7.             if n % prime == 0:
  8.                 isPrime = False
  9.                 break
  10.         if isPrime:
  11.             primes.append(n)
  12.     print("Primes until " + str(limit) + " ----> " + str(primes))
  13.     print()
  14.     return primes
  15.  
  16. # FUNCTION 2 - Goldbach
  17. def goldbach(limit):
  18.     primes = findPrimes(limit)
  19.     for n in range(4, limit, 2):
  20.         isThereASum = False
  21.         for prime1 in primes:
  22.             for prime2 in primes:
  23.                 if n == prime1 + prime2:
  24.                     isThereASum = True
  25.                     print(str(n) + " = " + str(prime1) + " + " + str(prime2))
  26.                     break
  27.         if isThereASum == False:
  28.             print("Error with number " + str(n) + ". Cannot find 2 primes with sum = " + str(n))
  29.             break
  30.  
  31.  
  32. # MAIN FUNCTION
  33. limit = 10**3
  34. goldbach(limit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement