add problem 25 solution

This commit is contained in:
Caleb Webber 2025-03-07 00:00:28 -05:00
parent 73a3e50ad5
commit 00acacb173
2 changed files with 30 additions and 0 deletions

View file

@ -23,6 +23,27 @@ defmodule Euler.Algebra do
Returns the sum of the digits of `n`. Returns the sum of the digits of `n`.
""" """
def digit_sum(n), do: digit_sum(n, 0) def digit_sum(n), do: digit_sum(n, 0)
def fib(0), do: 1
def fib(1), do: 1
def fib(n) do
cache = Process.get(:fib, %{})
case Map.get(cache, n, nil) do
nil ->
value = fib(n - 1) + fib(n - 2)
cache = Process.get(:fib, %{}) |> Map.put_new(n, value)
Process.put(:fib, cache)
value
m -> m
end
end
def num_digits(0, acc), do: acc
def num_digits(n, acc), do: num_digits(div(n,10),acc+1)
def num_digits(0), do: 1
def num_digits(n) do
num_digits(n, 0)
end
end end

View file

@ -0,0 +1,9 @@
import Euler.Algebra, only: [fib: 1, num_digits: 1]
Stream.iterate(1, &(&1 + 1))
|> Stream.map(&{&1, fib(&1)})
|> Stream.filter(&num_digits(elem(&1, 1)) >= 1000)
|> Enum.take(1)
|> Enum.map(&elem(&1, 0) + 1)
|> Enum.at(0)
|> IO.puts()