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

2

u/nthai Mar 02 '18 edited Mar 02 '18

Mathematica

So I just used some memoization to always remember the nth value and as usual, tried to avoid loops in Mathematica. Still not a really good solution because it recreates a list on each new call. Also had to increase the recursion limit for larger numbers.

Edit: there is no need to increase the recursion limit, I'm just dumb.

Recamán[0] = 0;
Recamán[n_] := Recamán[n] = (p = Recamán[n - 1] - n; If[p > 0 && ! MemberQ[Recamán /@ Range[0, n - 1], p], p, p + 2 n]);
Block[{$RecursionLimit = 5000}, Recamán /@ {5, 15, 25, 100, 1005}]

Output

{7, 24, 17, 164, 2683}