Skip to content

Instantly share code, notes, and snippets.

/pow.rb Secret

Created June 11, 2013 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/ee33ec3e1661a4408103 to your computer and use it in GitHub Desktop.
Save anonymous/ee33ec3e1661a4408103 to your computer and use it in GitHub Desktop.
/r/ruby question
def pow(base, exponent)
result = 1
i = 1
while i <= exponent
result = result * base
i += 1
end
result
end
puts "2 raised to the power 4 is..."
puts pow(2, 4)
puts "7 raised to the power 2 is..."
puts pow(7, 2)
# >> 2 raised to the power 4 is...
# >> 16
# >> 7 raised to the power 2 is...
# >> 49
@johnjansen
Copy link

def pow(base, exponent)
  result = 1
  exponent.times do
    result *= base
  end
  result
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment