# Make an n-Juggler

I've been really interested with sequences that follow the property

$a(n+1) = a(n - a(n))$

recently, so here's another question about these sequences. In particular we are concerned with sequences from the integers to the natural numbers.

A periodic sequence with the above property is an n-Juggler if and only if it contains exactly n distinct values. For example the following sequence is a 2 juggler

... 2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1 ...


because it only contains the numbers 1 and 2.

An example of a three juggler would be

... 3,5,3,5,1,5,3,5,3,5,1,5,3,5,3,5,1,5,3,5,3,5,1,5,3,5,3,5,1,5,3,5,3,5,1,5 ...


because it juggles 1, 3, and 5.

Given n > 1 as input, output any n-Juggler.

You may output a sequence in a number of ways, you can

• output a function that indexes it.

• take an additional input of the index and output the value at that index.

• output a continuous subsection of the sequence that, with the given property uniquely determines the sequence.

This is so answers are scored in bytes with less bytes being better.

• Are we allowed to print the sequence indefinitely? – Mr. Xcoder Oct 26 '17 at 15:31
• @Mr.Xcoder Normally I would say yes, but since these sequences are infinite in both directions, I have to hesitantly say no. (If you offer a compelling argument I could easily change my mind on this) If you can find a way to print in both directions that would be fine. – Wheat Wizard Oct 26 '17 at 15:33
• It seems the sequnces follow a(n+1) = a(n-a(n)), and not + – TFeld Oct 26 '17 at 16:05
• Yes it does: repeat 2,2 once → 2,2,2,2, repeat again → 2,2,2,2,2,2, etc. There's absolutely no way to get a 1 from repeating 2,2. The sequence you get is always unique. – aditsu quit because SE is EVIL Oct 26 '17 at 17:10
• It looks like the property is no more. – Erik the Outgolfer Aug 26 '18 at 19:24

# Pyth, 5 bytes

t+*%E


Try the 2, 3, 4 Jugglers.

Receives two numbers, N and I, separated by a newline and in this order. I is the index into the sequence.

This uses a quite simple formula: N - 1 + N * (I % N). Its validity was confirmed by the OP.

# CJam, 9

q~1$,=)*(  Try it online Input is n i where n is the main input and i is the index you want to get the value for. Explanation: q~ read and evaluate the input (n and i) 1$    copy n
,=    basically this is a modulo (i%n) that avoids a negative result for negative i
)*    increment, then multiply by n
(     decrement


# Haskell, 17 bytes

n!i=imodn*n+n-1


Try it online!

Another answer using Mr. Xcoder's idea. I wouldn't be able to answer the question otherwise to be honest. :P

# Python 2, 20 bytes

lambda n,i:i%n*n+n-1


Try it online!

Same principle as Mr. Xcoder's answer

• Should principal not rather read principle? – Jonathan Frech Aug 26 '18 at 20:28
• @JonathanFrech... yeah.. – TFeld Aug 27 '18 at 6:55

# Java 8, 15 bytes

n->i->i%n*n+n-1


Boring port of @Mr.Xcoder's Pyth answer.

Try it here.

# Jelly, 5 bytes

Ɠ%×+’


Try it online!

Port of Mr. Xcoder's approach. The index I is in STDIN, N is an argument.