# Make an infinite chain

Lets define a class of functions. These functions will map from the positive integers to the positive integers and must satisfy the following requirements:

• The function must be Bijective, meaning that every value maps to and is mapped to by exactly one value.

• You must be able to get from any positive integer to any other positive integer by repeated applications of the function or its inverse.

Now, write some code that will perform any one function in this class on its input.

This is a question so answers will be scored in bytes, with less bytes being better.

• Can you give an example?
– Jack
Jul 23, 2017 at 2:21
• @Jack Sure, Consider the function that maps 1 -> 2, maps every other odd number to that number minus 2, and every even number to that number plus 2. Jul 23, 2017 at 2:23
• Not sure I understand. Would f(x) := x+1 be a function of this class? In that case, 1+ in common lisp should do the trick here.
– dmh
Jul 23, 2017 at 11:39
• @MONODA43 That function is a bijection when considering all integers. But this question asks for a bijection on the positive integers. Your suggestion would fail to output 1 because there is no positive x for which x+1=1. Jul 23, 2017 at 13:57
• @Cowsquack You must be able to get from any positive integer to any other positive integer by repeated applications of the function or its inverse. You cannot get from 1 to 2 by applying the identity, no matter how many times you repeat. Jul 23, 2017 at 13:59

# Python 3, 24 bytes

lambda n:n-(-1)**n*2or 1


Try it online!

• Ha, you ninja'd me because I took 20 extra seconds to write out my sequence before posting... Jul 23, 2017 at 2:22

# Python, 22 bytes

lambda n:n+n%2*4-2or 1


The even numbers go down to the previous even number, odd numbers go up to the next odd number, and the mapping 2 -> 1 connects those two.

• wait... so 6 can result from 5 or 8? that's invalid? Jul 24, 2017 at 0:11
• @DestructibleLemon No, just a replaced odd with even in my explanation.
– orlp
Jul 24, 2017 at 1:30

## JavaScript (ES6), 20 bytes

n=>n>1?n%2?n-2:n+2:2


Maps ... -> 5 -> 3 -> 1 -> 2 -> 4 -> ..., which I think is valid. Correct me if I'm wrong...

• Alike minds think great. I'm using the inverse. Jul 23, 2017 at 2:20
• @Dennis Heh, and I've just realized that the inverse is 1 byte shorter: n=>n%2?n+2:n-2||1:2 Jul 23, 2017 at 2:21
• @Dennis You have been confirmed to be Yoda :o (StepHen's chat post) Jul 23, 2017 at 2:21
• @HyperNeutrino Greatly, alike minds think Jul 23, 2017 at 2:22

f 1=2
f x=x+2*(-1)^x


The example function from the OP's comment in Haskell. Try it online!

# Jelly, 6 bytes

-*Ḥạo1


Try it online!

### How it works

-*Ḥạo1  Main link. Argument: n

-*      Compute (-1)**n, yielding 1 for even n and -1 for odd n.
Ḥ     Unhalve; multipliy the result by 2.
ạ    Compute the absolute difference of the result and n.
o1  If the difference is 0, yield 1.

• Are we really letting Dennis get away with 'unhalve'? Haha.
– orlp
Jul 23, 2017 at 2:51

# Python 3, 43 bytes

lambda n,a=(1,3,-3,-1):n+a[n%4]if n-2else 1


Try it online!

And the inverse, also 43 bytes:

lambda n,a=(-3,-1,1,3):n+a[n%4]if n-1else 2


Try it online!

TIO links use Dennis's test footer code.

Since everyone is using one way of doing, I decided I'd be different, so I created by own (although I'm probably not the first person to discover it) function. This functions maps like ... 10 -> 7 -> 6 -> 3 -> 2 -> 1 -> 4 -> 5 -> 8 -> 9 -> ..., I can show you the hand drawing I made to test this if you would like.

My golfed algorithm may be probably is golfable - tips would be appreciated.

Ungolfed versions: Try it online!

# x86 32-bit machine code, 10 bytes

48 83 F0 01 74 01 40 34 01 C3


Try it online!

Uses the regparm(1) calling convention – argument in EAX, result in EAX.

Assembly:

.global f
f:
dec eax
xor eax, 1
jz skip
inc eax
skip:
xor al, 1
ret


# 05AB1E, 8 bytes

®¹m·αD_+


Try it online!

Exactly what Dennis's stuff does.

É4*+ÍD_+


Try it online!

orlp's stuff.

# Java (OpenJDK 8), 22 bytes

r->r%2>0?r<2?2:r-2:r+2


Try it online!

I wish Java had a ** exponentiation operator. Oh well!

# Vyxalr, 6 bytes

uedε1⟇


Try it Online!

@Dennis' answer, ported directly to Vyxal. Requires the r flag to reverse the arguments, e.g. ab+ evaluates to b+a instead of a+b.

Explanation taken from Dennis:

        # Implicit input -> n

ue      # Compute (-1)**n, yielding 1 for even n and -1 for odd n.
d     # Unhalve; multipliy the result by 2.
ε    # Compute the absolute difference of the result and n.
1⟇  # If the difference is 0, yield 1.

# Implicit output

• Very clever use of the r flag. Well done! Apr 27, 2021 at 0:00

# Mathematica, 21 bytes

a@1=2;a@b_:=b+2(-1)^b


Move along, nothing to see here...