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).

94 Upvotes

55 comments sorted by

View all comments

Show parent comment

2

u/chunes 1 2 Mar 04 '18

Factor

USING: kernel math prettyprint sequences ;
IN: dailyprogrammer.recaman

: a(n-1)|n ( seq -- a(n-1) n ) [ last ] [ length ] bi ;
: a(n-1)-n ( seq -- n )        a(n-1)|n - ;
: a(n-1)+n ( seq -- n )        a(n-1)|n + ;

: next-recaman ( seq -- n ) dup dup a(n-1)-n dup 0 >=
    [ swap member? not ] dip and [ a(n-1)-n ] [ a(n-1)+n ] if ;
    
: recaman ( n -- seq )
    V{ 0 } swap [ dup next-recaman over push ] times ;
    
: main ( -- ) { 5 15 25 100 1005 } dup last recaman swap
    [ swap nth . ] with each ;

MAIN: main

Output:

7
24
17
164
2683