31
\$\begingroup\$

Task

Given a positive integer n, output n+1 if n is odd, and output n-1 if n is even.

Input

A positive integer. You may assume that the integer is within the handling capability of the language.

Output

A positive integer, specified above.

Testcases

input output
    1      2
    2      1
    3      4
    4      3
    5      6
    6      5
    7      8
    8      7
  313    314
  314    313

Scoring

This is , so shortest answer in bytes wins.

Standard loopholes apply.

References

\$\endgroup\$
3
  • \$\begingroup\$ May we take input as unary? \$\endgroup\$
    – user41805
    May 4, 2017 at 10:12
  • 3
    \$\begingroup\$ This would be, surprisingly, a lot easier if it was the other way around in some languages \$\endgroup\$ May 5, 2017 at 17:41
  • 4
    \$\begingroup\$ @MistahFiggins That's well known enough that I'm pretty sure OP did it like this on purpose. \$\endgroup\$ May 5, 2017 at 20:51

79 Answers 79

1 2
3
0
\$\begingroup\$

Pyth, 10 bytes

+Q?%Q2 1_1

Try it online

As simple as using modulo of input with 2 and the ternary operator.

\$\endgroup\$
4
  • \$\begingroup\$ Golfed \$\endgroup\$
    – Leaky Nun
    May 5, 2017 at 11:14
  • \$\begingroup\$ Golfed. \$\endgroup\$
    – Leaky Nun
    May 5, 2017 at 11:15
  • \$\begingroup\$ How long did I search for standard short circuit D: only found reverse.. nice one though \$\endgroup\$
    – kalsowerus
    May 5, 2017 at 11:16
  • \$\begingroup\$ Welcome to PPCG, and welcome to Pyth. Hope you have a great time golfing in PPCG with Pyth. \$\endgroup\$
    – Leaky Nun
    May 5, 2017 at 11:19
0
\$\begingroup\$

Actually, 6 bytes

;0Dⁿ@-

Try it online, or run all test cases at once!

Explanation:

;0Dⁿ@-
;0Dⁿ    (-1)**n
    @-  n - (above)
\$\endgroup\$
0
\$\begingroup\$

Tcl, 30

proc s n {expr $n%2?$n+1:$n-1}

demo

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

Tcl, 24

based on @feersum's answer

 proc s n {expr -(-$n^1)}

demo


Tcl, 23

If it is a program instead of a function I can shave one byte off.

puts [expr -(-$argv^1)]

demo

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

Befunge-93, 11 bytes

&:2%2*1-+.@

[Try it online!]

Explanation:

Stack representation: bottom [A, B, C top

&:             Pushes 2 copies of the input, N:                   [N, N
  2%           Mods the top copy by 2, so it's either 1 or 0:     [N, (N % 2)
    2*         Multiplies that by 2, so that it's either 2 or 0:  [N, ((N % 2) * 2)
      1-       Subtracts 1, so it's either 1 or -1:               [N, (((N % 2) * 2) - 1)
        +      Adds the 2 together. -1 for evens, and 1 for odds: [(N-1) or [(N+1)
         .@    Prints and ends

Befunge-98 Variant, 10 bytes

This program can be trivially modified for Befunge-98 by removing the @, meaning the program will wrap around to the & and end:

&:2%2*1-+.

Try it online!

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

x86 machine code, 8 bytes

Load register al with an 8 bit integer

A8 01 (test al, 1; and whatever is in al with one, fast and small method for checking odd/even)
74 04 (jz 8; jump to even code if zf (zero flag, will be set if above instruction returns 0) is set)
04 02 (add al, 2; Two, because the next instruction will subtract one again, as if I incremented and jumped past the dec but shorter and faster)
FE C8 (dec al; Explained above, this is the code that is jumped to if input is even)

If you want, here is some actual assembly in boot-sector format. Just assemble this with nasm filename.asm and run qemu-system-i386 filename to test it. Press a key, and you will see that it will be the next letter if the ascii code was odd, or previous if it was even.

[ORG 0x7C00]

start:
xor ah, ah ;bios keyboard: get character (xoring a register with itself is a fast way to set it to 0 which is what we need)
int 0x16 ; keyboard io bios interrupt
mov ah, 0x0e ;bios graphics: display character (get ready)

test al, 0x01
jz even

add al, 2
even:
dec al
int 0x10 ; we already set al to 0E: display character, which takes a character from al and outputs that.

jmp start ; just go back to the start and get another character

times 510-($-$$) db 0 ; bootsector padding/signature
db 0x55
db 0xAA

Almost forgot, I should probably add the original assembly code that I used to create the bytes above

test al, 0x01
jz even

;odd code
add al, 2
even:
dec al
\$\endgroup\$
0
\$\begingroup\$

Clojure, 21 bytes

#(+(if(odd? %)1 -1)%)

It is quite difficult to get creative with this.

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

J-uby, 12 bytes

:-@|:^&1|:-@

Explanation

:-@|            negate n
    :^&1|       XOR it with 1
         :-@    negate that

With a change I just pushed to GitHub (inspired by this question, but overall useful), it becomes 10 bytes:

:-|:^&1|:-
\$\endgroup\$
0
\$\begingroup\$

@REXX 35 Bytes

arg a
x=pos(".",a/2)-1
say a+abs(x)/x

Explanation: Explanation: There is no distinction in Rexx between numbers and strings. The action you perform is what defines the type. The "typing" applies just to that action and can change at any time.

So, after dividing the number by 2 we then search for the decimal point. Subtract 1 from its position giving -1 if it was not found and a value in the range 1-n if it was. (Note that 1/2 returns 0.5 and not .5 so pos can never be 1 and therefore x can never be 0).

Try it here

REXX functions and instructions

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

,,,, 4 bytes

_1^_

Yay.

Explanation

_1^_

      input by command-line args
_     negate
 1    push 1
  ^   pop input and 1 and push input ^ 1 (bitwise XOR)
   _  negate
      implicit output
\$\endgroup\$
0
\$\begingroup\$

Haskell, 21 bytes

f x|odd x=x+1|0<1=x-1

I have no idea why I'm doing this xD

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

Common Lisp, 24 bytes

(lambda(x)(- x(expt -1 x)))

Try it online!

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

J, 9 bytes

(22 b.)&1

22 b. is XOR. Port of Cyoce's ruby answer.

Try it online!

Alternative straightforward answer:

+1:`_1:@.(2&|)

Try it online!

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

Keg, -hr, 7 bytes

:2%[;|⑨

Try it online!

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

Japt, 4 bytes

aJpU

Try it (includes all test cases)

aJpU     :Implicit input of integer U
a        :Absolute difference with
 J       :  -1
  pU     :  Raised to the power of U
\$\endgroup\$
0
\$\begingroup\$

Lua (40 bytes)

Using a table in order to avoid math.ceil or math.floor

function m(n)return n+({-1,1})[1+n%2]end
\$\endgroup\$
0
\$\begingroup\$

BRASCA, 7 bytes

ig{1_}n

Try it online!

Explanation

i       - Turn ASCII codepoints 48-57 to numbers 0-9
 g      - Concatenate stack
  {     - Decrement
   1_   - XOR by 1
     }  - Increment
      n - Output as number
\$\endgroup\$
0
\$\begingroup\$

TI-Basic, 7 bytes

Ans-i^(2Ans

Takes input in Ans. Output is stored in Ans and is displayed.


Alternatively, 9 bytes:

Ans-cos(2πfPart(Ans/2
\$\endgroup\$
0
\$\begingroup\$

><> (Fish), 10 bytes

:2%2*+1-n;

Try it

\$\endgroup\$
1 2
3

Your Answer

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

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