Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Numerical solution to Goldbach’s conjecture.
- Author: Helton Carlos de Souza <heltoncarlossouza@hotmail.com>
- The Goldbach’s Conjecture:
- 1) Every even number greater than 2 is the sum of two primes
- 2) Every odd number greater than 5 is the sum of three primes.
- """
- def divisors(n):
- """
- Return a divisors list of a number
- Ex.: Divisors of 12 == [1, 2, 3, 4, 6, 12]
- """
- return [x for x in range(1, n + 1) if n % x == 0]
- def is_prime(n):
- """
- Check if a number is prime (a prime number has as divisors only 1 and itself)
- """
- return divisors(n) == [1, n]
- def prime_range(n):
- """
- Return a list of prime numbers from 2 for n
- """
- return [x for x in range(2, n + 1) if is_prime(x)]
- def goldbach_conjecture(n):
- """
- Goldbach’s conjecture numerical solution
- Input => n: A integer value
- Return => List of tuples (the sum of these numbers is equal to the input n):
- The size of tuples in returned list is equal 2 (to even numbers > 2) or 3 (to odd numbers > 5)
- Obs.: The repeated pairs are removed. For instance, 8 = 3 + 5 or 5 + 3. To more readable answer, the last pair is removed, so 8 = 3 + 5
- """
- if n % 2 == 0:
- if n > 2:
- return [(x, y) for x in prime_range(n) for y in prime_range(n) if x + y == n and x <= y]
- else:
- raise ValueError('Even numbers must be greater than 2')
- else:
- if n > 5:
- return [(x, y, z) for x in prime_range(n) for y in prime_range(n) for z in prime_range(n) if x + y + z == n and x <= y <= z]
- else:
- raise ValueError('Odd numbers must be greater than 5')
- def show_goldbach_conjecture(n):
- """
- Show the goldbach_conjecture result in a "more friendly way"
- """
- tuples_list = goldbach_conjecture(n)
- print(str(n), end='')
- for t in tuples_list:
- print(" = ", end='')
- for index, number in enumerate(t):
- if index == len(t) - 1:
- print(str(number), end='')
- else:
- print(str(number) + " + ", end='')
- print()
- def test(number, expected):
- """
- Test if expected value is equal to the returned value in goldbach_conjecture() function
- """
- returned = goldbach_conjecture(number)
- if returned == expected:
- prefix = ' OK '
- else:
- prefix = 'FAIL'
- print('%4s [Number = %4d] returned: %s expected: %s' % (prefix, number, repr(returned), repr(expected)))
- print('-' * 100)
- def run_tests():
- """
- Automated tests for goldbach_conjecture() function
- """
- print ("Testing goldbach_conjecture() function...")
- test(6, [(3, 3)])
- test(7, [(2, 2, 3)])
- test(8, [(3, 5)])
- test(9, [(2, 2, 5), (3, 3, 3)])
- test(10, [(3, 7), (5, 5)])
- test(11, [(2, 2, 7), (3, 3, 5)])
- test(12, [(5, 7)])
- test(13, [(3, 3, 7), (3, 5, 5)])
- test(14, [(3, 11), (7, 7)])
- test(15, [(2, 2, 11), (3, 5, 7), (5, 5, 5)])
- test(42, [(5, 37), (11, 31), (13, 29), (19, 23)])
- test(100, [(3, 97), (11, 89), (17, 83), (29, 71), (41, 59), (47, 53)])
- test(150, [(11, 139), (13, 137), (19, 131), (23, 127), (37, 113), (41, 109), (43, 107), (47, 103), (53, 97), (61, 89), (67, 83), (71, 79)])
- test(200, [(3, 197), (7, 193), (19, 181), (37, 163), (43, 157), (61, 139), (73, 127), (97, 103)])
- test(1000, [(3, 997), (17, 983), (23, 977), (29, 971), (47, 953), (53, 947), (59, 941), (71, 929), (89, 911), (113, 887), (137, 863), (173, 827), (179, 821), (191, 809), (227, 773), (239, 761), (257, 743), (281, 719), (317, 683), (347, 653), (353, 647), (359, 641), (383, 617), (401, 599), (431, 569), (443, 557), (479, 521), (491, 509)])
- if __name__ == '__main__':
- run_tests()
- """
- Using Python interpreter...
- >> from goldbach_conjecture import goldbach_conjecture, show_goldbach_conjecture
- >> goldbach_conjecture(42)
- [(5, 37), (11, 31), (13, 29), (19, 23)]
- >> show_goldbach_conjecture(42)
- 42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement