Last active
October 1, 2015 11:49
-
-
Save alyssais/dd3796a1534ff6edf610 to your computer and use it in GitHub Desktop.
Calculate the prime summands of each even number greater than 4. (https://en.wikipedia.org/wiki/Goldbach%27s_conjecture)
This file contains hidden or 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
require "prime" | |
def prime_summands(n) | |
Prime.each do |x| | |
Prime.each do |y| | |
return x, y if x + y == n | |
break if y > n - 4 | |
end | |
break if x > n - 4 | |
end | |
nil | |
end | |
(4..1.0/0.0).step(2) do |n| | |
n = n.to_i | |
x, y = prime_summands(n) | |
if x | |
puts "#{n} = #{x} + #{y}" | |
else | |
puts "#{n} is not the sum of two primes" | |
exit | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment