Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

# October 31st, class 4 ## Fibonacci ```elixir defmodule Fibonacci do # def calc(0), do: 0 # def calc(1), do: 1 # def calc(n) when n > 1 do # calc(n - 1) + calc(n - 2) # end def find(nth) do list = [1, 1] fib(list, nth) end defp fib(list, 2) do Enum.reverse(list) end defp fib(list, n) do [first_element, second_element | _rest] = list fib([first_element + second_element | list], n - 1) end def nth_v2(n) do find(n) |> Enum.reverse() |> hd() end end ``` ```elixir Fibonacci.find(2000) ``` ```elixir Fibonacci.nth_v2(2000) ``` ```elixir defmodule HeronMethod do def sqrt(n, error \\ 1.0e-10) do sqrt(n, error, 1.0, 0.5 * (1 + n)) end defp sqrt(n, error, prev, new) when abs(new - prev) > error do sqrt(n, error, new, 0.5 * (new + n / new)) end defp sqrt(_, _, _, new), do: new def pow(base, exponent) do base ** exponent end def cbcrt(x) do x ** (1 / 3) end end ``` ```elixir ExUnit.start(auto_run: false) defmodule HeronMethodTest do use ExUnit.Case, async: false describe "Testing the square root function" do test "the square root of 9 is 3.0" do assert HeronMethod.sqrt(9) == 3.0 end end describe "Testing the power function" do test "3 raised to the power of 2 is 9" do assert HeronMethod.pow(3, 2) == 9 end end describe "Testing the cubic root function" do test "the cubic root of 27 is 3.0" do assert HeronMethod.cbcrt(27) == 3.0 end test "the cubic root of 64 is 4.0" do assert_in_delta HeronMethod.cbcrt(64), 4.0, 0.1 end end end ExUnit.run() ``` ```elixir HeronMethod.sqrt(16) ```
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More