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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
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
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #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!