34
\$\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
    Commented May 4, 2017 at 10:12
  • 4
    \$\begingroup\$ This would be, surprisingly, a lot easier if it was the other way around in some languages \$\endgroup\$ Commented May 5, 2017 at 17:41
  • 5
    \$\begingroup\$ @MistahFiggins That's well known enough that I'm pretty sure OP did it like this on purpose. \$\endgroup\$ Commented May 5, 2017 at 20:51

87 Answers 87

1 2
3
0
\$\begingroup\$

05AB1E, 7 bytes

D1(sm-Ä

Try it online!

Explanation:

D1(sm-Ä
               Input (implicit). Is 3 for example. Stack: [3]
D              Duplicate input. Stack: [3, 3]
  1            Push 1. Stack: [3, 3, 1]
    (          Push opposite of top of stack. Stack: [3, 3, -1]
      s        Swap the top 2 items on the stack. Stack: [3, -1, 3]
        m      Push -1**3. Stack: [3, -1]
           -   Subtract. Stack: [4].
            Ä  Absolute value. Stack: [4].
               Implicit output.
\$\endgroup\$
0
\$\begingroup\$

SAS, 72 bytes

%macro t(n);%put%eval(&n+%sysfunc(ifn(%sysfunc(mod(&n,2)),1,-1)));%mend;
\$\endgroup\$
0
\$\begingroup\$

dc, 10

?d2%2*+1-p

Uses the same formula as @TuukkaX's python answer.

Try it online.

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

Pyth, 4 bytes

I'm new to Pyth, so please tell me if there are any things I can golf.

_x1_

Explanation:

   _    Negate input
 x1     Bitwise XOR with 1
_       Negate
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Pyth! Hope you'll have a great time golfing in Pyth. \$\endgroup\$
    – Leaky Nun
    Commented May 5, 2017 at 6:02
0
\$\begingroup\$

Excel VBA 27 bytes

[a2]=iif(n mod 2=0,n-1,n+1)
\$\endgroup\$
1
  • \$\begingroup\$ I don't think taking input from a variable is legal on PPCG (the question's worded a bit weirdly, but I don't think that permits it). You'd have to write a program or function. (Also, although it's optional, I recommend writing an explanation or brief description of how your code works; posts like this one which are just a code block have a tendency to set off the low-quality-posts filter, which can cause trouble sometimes.) \$\endgroup\$
    – user62131
    Commented May 5, 2017 at 11:11
0
\$\begingroup\$

QBIC, 15 10 bytes

:?a-(-1)^a

Saved a lot by using a different calculation.

Explanation:

:           Get a number frm the cmd line as var a
?           PRINT
 a-(-1)^a   a decreased by 1 for when even, or -1 (adding one) when odd.

QBasic (and QBIC) need the parenteses around (-1), because code like -1^2 is otherwise seen as negate 1*1 = negate 1 = -1...

\$\endgroup\$
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
    Commented May 5, 2017 at 11:14
  • \$\begingroup\$ Golfed. \$\endgroup\$
    – Leaky Nun
    Commented 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
    Commented 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
    Commented 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\$

@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\$

Vyxal, 2 bytes

Ǎ-

Try it Online!

 - # n -
Ǎ  # -1 ^ n
\$\endgroup\$
0
\$\begingroup\$

Uiua 0.11.0, 5 bytes SBCS

-ⁿ,¯1

Try on Uiua Pad!

Explanation

,¯1 # n, -1, n 
ⁿ   # (-1)^n, n
-   # n - (-1)^n
\$\endgroup\$
0
\$\begingroup\$

AWK, 13 bytes

$0+=$0%2?1:-1

Try it online!

$0+=   # set output
$0%2   # parity
?1:-1  # add or subtract
\$\endgroup\$
0
\$\begingroup\$

ARBLE, 7 bytes

-(-n~1)

Try it online!

Humble port of the C answer

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

Raku (Perl 6) (rakudo), 18 bytes

f($x){$x-(-1)**$x}

Attempt This Online!

\$\endgroup\$
1 2
3

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.