Last active
September 7, 2016 08:44
-
-
Save radik909/3c53fe1e835c6ee0de8d93e2a4750b74 to your computer and use it in GitHub Desktop.
Elixir - Factorial for a given non-negative number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Factorial do | |
@moduledoc """ | |
Factorial for a given non-negative number | |
""" | |
def run(0), do: 1 | |
def run(num) when num > 0 do | |
Enum.reduce 1..num, 1, &(&1 * &2) | |
end | |
def run(_), do: nil | |
end | |
IO.gets("Enter the number: ") | |
|> String.strip | |
|> String.to_integer | |
|> Factorial.run | |
|> IO.puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment