Skip to content

Instantly share code, notes, and snippets.

@karapetyan
Created October 11, 2015 19:38
Show Gist options
  • Save karapetyan/901ce0b98983ae41f833 to your computer and use it in GitHub Desktop.
Save karapetyan/901ce0b98983ae41f833 to your computer and use it in GitHub Desktop.
def pentagon
pentagon = []
i = 1
loop do
pentagon << i * (3 * i - 1) / 2
pentagon.each_with_index do |item, index|
for b in index+1..pentagon.size-1
puts "P1 #{pentagon[index]} P2 #{pentagon[b]}" if in_penta?(pentagon[index] + pentagon[b]) && in_penta?(pentagon[b] - pentagon[index])
end
end
i += 1
end
end
private
def in_penta?(num)
i = 1
loop do
penta = i * (3 * i - 1) / 2
if num == penta
return true
elsif penta > num
return false
else
i += 1
end
end
end
pentagon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment