r/dailyprogrammer • 2 0 • Mar 02 '18

Weekly #28 - Mini Challenges

So this week, let's do some mini challenges. Too small for an easy but great for a mini challenge. Here is your chance to post some good warm up mini challenges. How it works. Start a new main thread in here.

if you post a challenge, here's a template we've used before from /u/lengau for anyone wanting to post challenges (you can copy/paste this text rather than having to get the source):

**[CHALLENGE NAME]** - [CHALLENGE DESCRIPTION]

**Given:** [INPUT DESCRIPTION]

**Output:** [EXPECTED OUTPUT DESCRIPTION]

**Special:** [ANY POSSIBLE SPECIAL INSTRUCTIONS]

**Challenge input:** [SAMPLE INPUT]

If you want to solve a mini challenge you reply in that thread. Simple. Keep checking back all week as people will keep posting challenges and solve the ones you want.

Please check other mini challenges before posting one to avoid duplications (within reason).

97 Upvotes

55 comments sorted by

View all comments

Show parent comment

3

u/Kendrian Mar 07 '18 edited Mar 07 '18

Golfed in Julia:

rseq(n) = n == 0 ? ([0], Set(0)) : begin
    (seq, vals) = rseq(n-1)
    x = seq[end] - n
    x <= 0 || x in vals ? x += 2*n : nothing
    (push!(seq, x), push!(vals, x))
end
    
recaman(n) = rseq(n)[1][end]

map(recaman, [5, 15, 25, 100, 1005])

Or, the slightly less golfed but now resistant to stack overflow version:

rseq(n) = n == 0 ? [0] : begin
    (s, v) = ([0], Set(0))
    for i = 1:n
        x = s[end] - i
        x <= 0 || x in v ? x += 2*i : nothing
        (push!(s, x), push!(v, x))
    end
    s
end

This computes the sequence to 10_000_000 terms in about 2 and a half seconds.