- Prefer latest node version(>=8.0)
- Use yarn over npm
- Use Prettier for formatting your code.
- Integrate
Prettier
with your editor.
Prettier
with your editor.defmodule Fibonacci do | |
def run(num) when num > 0 do | |
Stream.unfold({1, 1}, fn {a, b} -> | |
{a, {b, a + b}} | |
end) | |
|> Enum.take(num) | |
end | |
end | |
IO.gets("Enter the limit: ") |
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 |
defmodule Prime do | |
@moduledoc """ | |
A number 'n' is prime only when it is not divisible by any number between 2 and √n | |
""" | |
def run(2), do: true | |
def run(num) when num > 2 and rem(num, 2) != 0 do | |
is_prime(3, num, round(:math.sqrt(num) + 1)) | |
end | |
def run(_), do: false |