find the nth decagonal number in ruby

To find the nth decagonal number, we can use the formula:

Dn = 3n^2 - 2n

where n is the index of the decagonal number.

We can write a Ruby function that takes an integer n as input and returns the nth decagonal number using this formula:

main.rb
def decagonal_number(n)
  3 * n**2 - 2 * n
end
47 chars
4 lines

Here's an example of how we can use this function:

main.rb
# find the 5th decagonal number
puts decagonal_number(5)  # output: 133
72 chars
3 lines

This will output 133, which is the 5th decagonal number.

gistlibby LogSnag