Logo

Programming-Idioms

History of Idiom 66 > diff from v10 to v11

Edit summary for version 11 by :

Version 10

2015-09-03, 16:29:11

Version 11

2015-09-04, 13:30:03

Idiom #66 Big integer exponentiation

Calculate the result z of x power n, where x is a big integer and n is a positive integer.

Idiom #66 Big integer exponentiation

Calculate the result z of x power n, where x is a big integer and n is a positive integer.

Code
def raise(_, 0) do
    1
  end

  def raise(x, 1) do
    x
  end

  def raise(x, n) when n > 0 do
    IO.puts("Enter with x = #{x}, n = #{n}")
    result = x * raise(x, n - 1)
    IO.puts("Result is #{result}")
    result
  end
Origin
http://chimera.labs.oreilly.com/books/1234000001642/apa.html#_literal_powers_traced_ex_literal