To find the nth hexagonal number in Ruby, we can use the formula:
Hexagonal number = n(2n-1)
We can create a method that takes the nth value as an argument and calculates and returns the hexagonal number. Here is the implementation:
main.rb79 chars5 linesNow we can call this method and pass the nth value as an argument to find the corresponding hexagonal number:
main.rb122 chars4 linesOutput:
main.rb32 chars2 lines
In the above example, we passed 5 as the nth value and the program returned 85 as the corresponding hexagonal number.
gistlibby LogSnag