find the nth pentagonal number in ruby

Math, pentagonal number, sequence, loop, ruby

Method to find the nth pentagonal number

def get_pentagonal_number(n) return n*(3*n-1)/2 end

Use the method to find the 10th pentagonal number

puts get_pentagonal_number(10)

main.rb
Here, we have used the formula for nth pentagonal number `n(3n-1)/2`. The formula is enclosed in a method called `get_pentagonal_number`. We passed `10` as an argument to our method to find the 10th pentagonal number. The output will be `145`.
244 chars
2 lines

related categories

gistlibby LogSnag