This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 3digit * 3digit, which is palindrome | |
defmodule Problem4 do | |
def palindrome?(n) do | |
s = to_string(n) | |
String.reverse(s) == s | |
end | |
def solve do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# largest factor of 600851475143 | |
defmodule Problem3 do | |
# http://wende.github.io/2015/09/07/Elixir-n-prime-numbers.html | |
def prime?(x), do: (2..round(:math.sqrt(x)) |> Enum.filter(fn a -> rem(x, a) == 0 end) |> length()) == 0 | |
def solve(num) do | |
(2 .. round(:math.sqrt(num))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sum of fibonacci numbers which is even and less than 4000000 | |
# https://elixirforum.com/t/project-euler-problem-2/500/2 | |
defmodule Problem2 do | |
def fib_seq do | |
Stream.unfold({1,1}, fn {a,b} -> {a, {b, a + b}} end) | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# less than 1000, multiple of 3 or 5 | |
defmodule Problem1 do | |
def solve(limit) do | |
1..(limit-1) | |
|> Enum.filter(fn x -> ((rem(x, 3) == 0) || (rem(x, 5) == 0)) end) | |
|> Enum.reduce(0, &(&1 + &2)) | |
end | |