find the nth heptagonal number in ruby

#ruby, math, sequence, geometry, heptagonal

def nth_heptagonal_number(n) n * (5 * n - 3) / 2 end

Example usage:

nth_heptagonal_number(5) #=> 95

main.rb

The formula to find the nth heptagonal number is `n(5n-3)/2`. We can simply define a method that takes in the nth heptagonal number we want to find and applies this formula.
175 chars
3 lines

related categories

gistlibby LogSnag