Skip to content

Instantly share code, notes, and snippets.

@alyssais
Last active October 1, 2015 11:49
Show Gist options
  • Save alyssais/dd3796a1534ff6edf610 to your computer and use it in GitHub Desktop.
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)
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