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.

  • 1
    \$\begingroup\$ Are we allowed to print the sequence indefinitely? \$\endgroup\$
    – Mr. Xcoder
    Oct 26, 2017 at 15:31
  • 4
    \$\begingroup\$ @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. \$\endgroup\$
    – Wheat Wizard
    Oct 26, 2017 at 15:33
  • 3
    \$\begingroup\$ It seems the sequnces follow a(n+1) = a(n-a(n)), and not + \$\endgroup\$
    – TFeld
    Oct 26, 2017 at 16:05
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ Oct 26, 2017 at 17:10
  • 2
    \$\begingroup\$ It looks like the property is no more. \$\endgroup\$ Aug 26, 2018 at 19:24

7 Answers 7


Pyth, 5 bytes


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

How about this?


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.


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


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

  • 1
    \$\begingroup\$ Should principal not rather read principle? \$\endgroup\$ Aug 26, 2018 at 20:28
  • \$\begingroup\$ @JonathanFrech... yeah.. \$\endgroup\$
    – TFeld
    Aug 27, 2018 at 6:55

Vyxal, 4 bytes


Try it Online!

  • \$\begingroup\$ without the -1 your function obeys \$a(n)=a(n-a(n))\$. \$\endgroup\$
    – att
    Jun 10, 2022 at 4:14

Java 8, 15 bytes


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.