Skip to content

Instantly share code, notes, and snippets.

@internetsadboy
Last active August 29, 2015 14:11
Show Gist options
  • Save internetsadboy/9ae90bcf67cb61ad72af to your computer and use it in GitHub Desktop.
Save internetsadboy/9ae90bcf67cb61ad72af to your computer and use it in GitHub Desktop.
for n => return [p1, p2] ; where n is an even integer and p1 and p2 are two prime integers that summate to n.
require 'prime'
# return two prime numbers that summate to n, where n is an even integer
def primes (n)
if n % 2 != 0
return "[ERROR] not an even number"
end
# identify primes
prs = []
for i in 0...n
if Prime.prime?(i)
prs.push(i)
end
end
# find summation values
for j in 0...prs.length
for k in 0...prs.length
if prs[j] + prs[k] == n
return prs[j], prs[k]
end
end
end
return "[ERROR] something bad happened"
end
p primes(100) # [3, 97]
p primes(9) # [ERROR] not an even number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment