Created
June 4, 2011 16:34
-
-
Save johana-star/1008043 to your computer and use it in GitHub Desktop.
Euler Project Problem #045
This file contains hidden or 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
# 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