Skip to content

Instantly share code, notes, and snippets.

@radik909
Last active September 7, 2016 08:44
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 radik909/3c53fe1e835c6ee0de8d93e2a4750b74 to your computer and use it in GitHub Desktop.
Save radik909/3c53fe1e835c6ee0de8d93e2a4750b74 to your computer and use it in GitHub Desktop.
Elixir - Factorial for a given non-negative number
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