Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #It was proposed by Christian Goldbach that every odd composite number can be written as the sum of
- # a prime and twice a square.
- #9 = 7 + 2x1**2
- #15 = 7 + 2x2**2
- #21 = 3 + 2x3**2
- #25 = 7 + 2x3**2
- #27 = 19 + 2x2**2
- #33 = 31 + 2x1**2
- #It turns out that the conjecture was false.
- #What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
- def isPrime(n):
- '''check if integer n is a prime'''
- if n == 2:
- return True
- elif n < 2 or n%2 == 0:
- return False
- # range starts with 2 and only needs to go up the square-root of n
- for x in range(2, int(n**0.5)+1):
- if n % x == 0:
- return False
- return True
- def isPerfectSquare(n):
- if n < 0:
- return False
- result = int(n**0.5)
- return result*result == n
- odd = 9
- flag = True
- while flag:
- if isPrime(odd): #Exluding prime odds from test (check every odd composite number)
- odd += 2
- oddPass = False
- for i in range(2,odd-1):
- if isPrime(odd - i) and i%2 == 0 and isPerfectSquare(i/2):
- #checking if there is a number such that odd-number=prime
- #and if that number is even and number/2 is perfect square
- oddPass = True
- break
- if oddPass:
- odd += 2
- else:
- print odd
- flag = False
Advertisement
Add Comment
Please, Sign In to add comment