Created
October 11, 2015 19:38
-
-
Save karapetyan/901ce0b98983ae41f833 to your computer and use it in GitHub Desktop.
This file contains 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
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