Skip to content

Adding Elixir, made changes to Readme #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
478 changes: 417 additions & 61 deletions README.md

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions logos/elixir.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions src/elixir/CalculatePi.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Module to calculate an approximation of Pi
defmodule PiApproximation do

# Function to calculate pi given number of terms
def calculate_pi(terms) do

# Initialize pi accumulator
pi = 0.0

# Reduce over 1..terms range
# Accumulator is a tuple with pi, denominator, and operation
Enum.reduce(1..terms, {pi, 1.0, 1.0}, fn _, {acc_pi, acc_denom, acc_op} ->

# Calculate new pi value
new_pi = acc_pi + acc_op * (4.0 / acc_denom)

# Return tuple with updated values
{new_pi, acc_denom + 2.0, -acc_op}
end) |> elem(0)
end

end

# Number of terms to approximate
terms = 100_000

# Call approximation function
result = PiApproximation.calculate_pi(terms)

# Print result
IO.puts(result)
26 changes: 26 additions & 0 deletions src/elixir/FactorialIterative.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Factorial do
# Define a module called Factorial.

def factorial(n) when n >= 0 do
# Calculate the factorial using Enum.reduce.
Enum.reduce(1..n, 1, &(&1 * &2))
end

defp format_factorial(i, result) do
# Format the output string.
"#{i}! = #{result}"
end

def main do
# Entry point of the program.
nums = [0, 1, 2, 3, 4, 5]

Enum.each(nums, fn i ->
# Iterate through the list of numbers.
result = factorial(i)
IO.puts(format_factorial(i, result))
end)
end
end

Factorial.main() # Execute the program.
23 changes: 23 additions & 0 deletions src/elixir/FactorialRecursive.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Define Math module for math utilities
defmodule Math do

# Recursive factorial function
def factorial(0), do: 1 # Base case

def factorial(n) when n > 0 do
# Recursive case
n * factorial(n-1)
end

end

# Array of numbers to get factorials for
nums = [0, 1, 2, 3, 4, 5]

# Loop through the nums array
for i <- nums do

# Calculate and print factorial
IO.puts "#{i}! = #{Math.factorial(i)}"

end
24 changes: 24 additions & 0 deletions src/elixir/FibonacciIterative.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Define a Fibonacci module
defmodule Fib do

# Function to calculate nth Fibonacci number
def fibonacci(n) when n >= 0 do

# Initialize starting values
last = 0
curr = 1

# Use Enum.reduce as a loop from 0 to n-1
Enum.reduce(0..(n-1), {last, curr}, fn _, {last, curr} ->

# Update last and curr, returning as a tuple
{curr, curr + last}
end)

# Return the last value after reducing
|> elem(0)
end
end

# Print 10th Fibonacci number
IO.puts(Fib.fibonacci(10))
14 changes: 14 additions & 0 deletions src/elixir/FibonacciRecursive.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Define a Fibonacci module
defmodule Fibonacci do
# Base case: If n is 0 or 1, return n.
def fibonacci(n) when n <= 1, do: n

# Recursive case: Calculate the Fibonacci number by summing the results
# of the previous two Fibonacci numbers.
def fibonacci(n) do
fibonacci(n - 1) + fibonacci(n - 2)
end
end

# Print the Fibonacci number for n = 10.
IO.puts("Fibonacci(n): #{Fibonacci.fibonacci(10)}")
19 changes: 19 additions & 0 deletions src/elixir/MaxRecursive.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule MaxRecursive do
# Base case: When n is 1, return the first element of the list (head).
def max_recursive(nums, 1), do: hd(nums)

# Recursive case: Calculate the maximum recursively.
def max_recursive(nums, n) do
aux = max_recursive(nums, n - 1)
if aux > Enum.at(nums, n - 1) do
aux
else
Enum.at(nums, n - 1)
end
end
end

# Example usage
nums = [1, 2, 3, 4, 32, 6, 7, 8, 9, 10]
IO.inspect "nums = #{inspect(nums)}"
IO.inspect "Max = #{MaxRecursive.max_recursive(nums, length(nums))}"