15
\$\begingroup\$

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.

\$\endgroup\$
9
  • 1
    \$\begingroup\$ Can you give an example? \$\endgroup\$
    – Jack
    Jul 23, 2017 at 2:21
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Wheat Wizard
    Jul 23, 2017 at 2:23
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – dmh
    Jul 23, 2017 at 11:39
  • 2
    \$\begingroup\$ @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. \$\endgroup\$
    – kasperd
    Jul 23, 2017 at 13:57
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – kasperd
    Jul 23, 2017 at 13:59

11 Answers 11

10
\$\begingroup\$

Python 3, 24 bytes

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

Try it online!

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

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.

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

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

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

Haskell, 20 bytes

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

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

\$\endgroup\$
4
\$\begingroup\$

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.
\$\endgroup\$
1
  • 14
    \$\begingroup\$ Are we really letting Dennis get away with 'unhalve'? Haha. \$\endgroup\$
    – orlp
    Jul 23, 2017 at 2:51
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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

Graph of how this affects numbers

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

05AB1E, 8 bytes

®¹m·αD_+

Try it online!

Exactly what Dennis's stuff does.

É4*+ÍD_+

Try it online!

orlp's stuff.

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

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!

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

Vyxal r, 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
\$\endgroup\$
1
  • \$\begingroup\$ Very clever use of the r flag. Well done! \$\endgroup\$
    – lyxal
    Apr 27, 2021 at 0:00
0
\$\begingroup\$

Mathematica, 21 bytes

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

Move along, nothing to see here...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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