Logo

Programming-Idioms

History of Idiom 66 > diff from v323 to v324

Edit summary for version 324 by martinsvalin:
[Elixir] Exponentiation is in the standard library. Doing it recursively is non-idiomatic.

Version 323

2016-05-28, 23:57:38

Version 324

2016-09-29, 21:24:29

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
Code
z = :math.pow(x, n)
Origin
http://chimera.labs.oreilly.com/books/1234000001642/apa.html#_literal_powers_traced_ex_literal