Hey, everyone. If yesterday was our rest day, today is the warm-up.
We have another great challenge from user g964 on CodeWars:
Consider a sequence
u
, where u is defined as follows:
- The number
u(0) = 1
is the first one inu
.- For each
x
inu
,y = 2 * x + 1
andz = 3 * x + 1
must also be inu
.- There are no other numbers in
u
.Ex:
u = [1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]
1 gives 3 and 4. 3 gives 7 and 10 while 4 gives 9 and 13. This pattern continues as far you as allow it to.
Your task is to create a function
dbl_linear
with a parameter ofn
that returns the elementu(n)
in an ordered sequence (excluding duplicates).
Good luck! Let us know if you like these mathematical challenges and we'll stick with the trend.
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (18)
For n = 100,000 , on an ok laptop, it runs in roughly 45 seconds
Here's my solution in Perl. I actually think its a bit too slow, even when using
state
variables to cache the results in subsequent calls. Maybe I'll look into optimizing it later.I'll write an explainer for it when I get a few minutes later.
Here is my Rust solution! I think my answer is pretty good except I should be using a better data structure than a
Vec
here, since I really want to push/pop from both ends. If I did that I would be able to avoid needing to sort and reverse my vec, which would help on the processing speed!Take a look at std::collections::VecDeque :)
Thank you! I knew there was one that fit the bill better!
Nim
Time:
./main 0.04s user 0.01s system 39% cpu 0.113 total
Not familiar with nim. It looks like result is a list, and that duplicate values are being added to it. Is that not the case?
I think you're right! Was testing with too small of an input-set to see any duplicates. :)
Cheap fix (./main 341.16s user 0.52s system 99% cpu 5:42.40 total):
JavaScript
I've been running late lately... but here is a live demo on CodePen
This one times out on bigger input because of generating
Object.keys(series)
every time inside the loopI like em, but I think it's fun to have a good mix of different types of challenges!
One suggestion is to provide more examples/test cases. Especially for the mathy ones, where the correct answers might not be super obvious to everyone, having many examples/test cases can definitely make it easier! Just a thought!
Not sure I have well understood this one ... Am I correct with this JS function ? 🙈
EDIT: added the line
u = Array.from(new Set(u))
to get unique values !Not too good with maths, hope I understood well. Here is a (very inefficient, so I added a constraint to the max num to give) attempt in bash:
Haskell, featuring tail recursion!
A little late to the party (3am my time), but oh well! Here's my submission:
Full code w/ some tests: gist.github.com/kerrishotts/029c8f...
This is one where it would have been super helpful to have some answers to compare against. Beyond the initial few digits, I'm just assuming things are correct, which may not be true.