Skip to content

Instantly share code, notes, and snippets.

@johana-star
Created June 4, 2011 16:34
Show Gist options
  • Save johana-star/1008043 to your computer and use it in GitHub Desktop.
Save johana-star/1008043 to your computer and use it in GitHub Desktop.
Euler Project Problem #045
# Solution to Project Euler's forty-fifth problem
# http://projecteuler.net/index.php?section=problems&id=45
# Find the second number which is a triangle number, pentagonal number, and hexagonal number.
def create_pentagonal_number(number)
pentagonal = (number*(3*number-1))/2
return pentagonal
end
def create_hexagonal_number(number)
hexagonal = number*(2*number-1)
return hexagonal
end
def compare_hex_and_pent_numbers(one, two)
pent = create_pentagonal_number(one)
hex = create_hexagonal_number(two)
compare = 2
until compare == 0
compare = pent <=> hex
case compare
when 0
p pent
when -
one += 1
pent = create_pentagonal_number(one)
when 1
two += 1
hex = create_hexagonal_number(two)
end
end
end
a = Time.new
pent_count = 166
hex_count = 144
compare_hex_and_pent_numbers(pent_count, hex_count)
b = Time.new - a
p b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment