10
\$\begingroup\$

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.

Task

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.

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

7 Answers 7

5
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

CJam, 9

How about this?

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
\$\endgroup\$
1
\$\begingroup\$

Haskell, 17 bytes

n!i=i`mod`n*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

\$\endgroup\$
1
\$\begingroup\$

Python 2, 20 bytes

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

Try it online!

Same principle as Mr. Xcoder's answer

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

Vyxal, 4 bytes

%›*‹

Try it Online!

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

Java 8, 15 bytes

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

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

Try it here.

\$\endgroup\$
0
\$\begingroup\$

Jelly, 5 bytes

Ɠ%×+’

Try it online!

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

\$\endgroup\$

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.