sprengerjo
Hi there,
i am new to elixir, so i solved some stuff on codewars.
i ran into this kata: Twice linear | Codewars
I tried to solve storing the u in a list, and learned linked list are not a good idea to solve that thing ![]()
I could not get it working with tuples. I will push the solutions to github asap.
My fastest solution is based on a map cache, that is still to slow, i.e.
dotest(1000000, 54381286) # ~ 6-7 seconds
dotest(100000, 2911582) # ~ 300 ms
I get a timeout after 30 of 100 random tests…
def dbl_linear(n) do
{_, un} = Enum.reduce(
1..n,
{%{0 => 1}, 1},
fn (i, {u, un}) ->
x = Integer.floor_div((un - 1), 2)
y = Integer.floor_div((un - 1), 3)
xx = binary(x, u, 0, i - 1)
yy = binary(y, u, 0, i - 1)
un = 1 + Kernel.min(2 * xx, 3 * yy)
u = Map.put(
u,
i,
un
)
{
u,
un
}
end
)
un
end
defp binary(0, _, _, _), do: 1
defp binary(_, cache, lb, ub) when ub - lb <= 1 do
%{^ub => un} = cache
un
end
defp binary(un, cache, lb, ub) do
mid = lb + (
(ub - lb)
>>> 1)
%{^mid => mid_v} = cache
if mid_v > un do
binary(un, cache, lb, mid)
else
binary(un, cache, mid, ub)
end
end
Trending in Challenges
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
peerreynders
Really?
Spoiler
Refined version:
Spoiler
sprengerjo
Using 2 lists instead of 2 index variables does the trick. I will refactor my solution according to that.
Thanks for your help, Peer! Much appreciated!