Is this even or odd?

Note: There is not been a vanilla parity test challenge yet (There is a C/C++ one but that disallows the ability to use languages other than C/C++, and other non-vanilla ones are mostly closed too), So I am posting one.

Given a positive integer, output its parity (i.e. if the number is odd or even) in truthy/falsy values. You may choose whether truthy results correspond to odd or even inputs.

Examples

Assuming True/False as even and odd (This is not required, You may use other Truthy/Falsy values for each), responsively:

(Input):(Output)
1:False
2:True
16384:True
99999999:False

• This isn't the first time I've confused mathematical with computational parity... this is a code site after all! – Neil Mar 21 '17 at 10:38
• Since this is pretty much one of these(1,2,3) questions, it should probably have a snippet to see all the answers. – fəˈnɛtɪk Mar 21 '17 at 13:57
• @MikeBufardeci Because "catalogue" is spelled differently based on which country you're from. For those of us in the U.S., it's "catalog". "Leaderboard" is culture-invariant. – mbomb007 Mar 21 '17 at 16:37
• @tuskiomi The challenge only asks about positive integers. (0 is considered even but not positive) – Calvin's Hobbies Mar 22 '17 at 2:46
• @LucioCrusca Welcome to PPCG! The basic idea of Code Golf is to make a program in the shortest form you can. This challenge is to read an integer (positive,non-zero), and output if it is even or odd. If you are confused with something, please visit The Nineteenth Byte and ask freely. Or if you are confused with the site's policy or rules, go to the Meta. Finally, Thanks for subscribing to our community! – Matthew Roh Mar 23 '17 at 15:09

isodd

Try it online!

><>, 4 + 3 = 7 bytes

Outputs 1 for odd and 0 for even. Execute using -v (+ 3 bytes).

2%n;

Try it online!

QBIC, 7 5 bytes

:?a%2

This prints 0 for even values, and 1 for odds.

Explanation:

:   get a number from the cmd line args
?   PRINT
a%2 The input modulo 2 (1 for odds, 0 even)

dc, 4 bytes

Output is 1 for odd, and 0 for even.

?2%p     # read input, push 2, do modulo, print

Try it online!

I think for stack-based languages, I can assume the input is already on the stack, and that I can leave the output on top when done. This would be similar to how I/O is allowed for functions.

Stack I/O: 2 bytes

2%

yup, 15 bytes

{0e0:e---}00e--

Try it online!

Much nicer than I thought it might be! I don't presently have a modulus algorithm (though there most likely is one), but this is considerably simpler. It takes input from the stack and leaves output on the stack.

Explanation

{0e0:e---}00e--   stack     | explanation
[n]       | input from top of stack
{        }        while top of stack is positive:
0                [n 0]     | push 0
e               [n 1]     | pop x, push e^x
0              [n 1 0]   | push 0
:             [n 1 0 0] | duplicate
e            [n 1 0 1] | pop x, push e^x
-           |n 1 -1]  | subtract
-          [n 2]     | subtract
-         [n-2]     | subtract
now, either -1 is on the stack for odd, or 0 for even
0       [c 0]     | push 0
0      [c 0 0]   | push 0
e     [c 0 1]   | pop x; push e^x
-    [c -1]    | subtract

Befunge 98, 4 bytes

&2%.

Try it Online! (It may take a while because of how it ends)

&       Take input
2%     Mod 2
.    Print
&       The IP wraps around and hits the "take input" again. Because of the
way TIO's '98 handles & on EOF, this ends the program after a minute.

This answer is very similar to Befunge 93, but doesn't need the @ to end.

Befunge 93, 5 bytes

&2%.@

Try it Online!

&        Take input
2%      Mod 2
.@    Print and exit

This answer is very similar to Befunge 98, but it needs the @ to end because & pushes the last number in the input on EOF

V, 87 6 bytes

Àé1òhD

Try it online!

Outputs 1 for odd numbers and nothing (an empty string) for even numbers.

Àé1                   " argument times insert a 1
" this converts input to unary
" and now the cursor is at the end of the line
ò                  " recursively do:
h                 "  move 1 to the left and
D                "  delete everything from the cursor to the end of the line
"  this effectively removes 2 characters at once until
"  a breaking error occurs at which point
" implicit ending ò
• Cool approach, I like it! Alternate (but very similar) approach: Àé1Ó11 – DJMcMayhem Mar 21 '17 at 20:34

{!it%2}

J, 3 bytes

2&|

Explanation:

2&|
|    this does mod, only with the order of the operands reversed
2      the number 2... derp
&     attaches 2 to | so it can be the left operand
the right operand is implied due to tacit programming
• Alternatively, 2|]. – Conor O'Brien Mar 21 '17 at 18:37
• um... what does the ']' do? – Bijan Mar 21 '17 at 18:51
• Well, it means "yield right argument", and is often the identity function. A train of three verbs (or a constant followed by two verbs, as in this case) constitutes a "fork", in which the middle "tine" of the verb (in this case, %) is applied the result of the application on each of the outer tines. If a constant is invoked in this way, it simply returns the constant. So, for an argument y, (2|])y is equivalent to (2 | (] y)), which is 2 | y. – Conor O'Brien Mar 21 '17 at 19:24
• An APL expression for all the 3-byte J expressions that are valid answers – Adám Mar 23 '17 at 10:59

Bash + coreutils, 11 bytes

Output is 1 for odd numbers, and nothing for even ones. This is a full program. There is another bash answer by Digital Trauma, but that one is a function, however it is in pure bash.

seq $[$1%2]          # give 'input % 2' as argument to seq. Notice that 'seq 0'
#doesn't print anything.

Try it online!

I use seq, because it is 1 byte shorter than echo, but it has a different truthy / falsy mapping.

tinylisp, 18 bytes

There's a library function for that.

The function itself is odd?--call it like (odd? 12). Try it online!

Writing a function from scratch is 28 bytes (TIO):

(d o(q((n)(i(l n 2)n(o(s n 2

This defines o as: if n is less than 2, return n, else recurse with n-2.

LibreLogo, 19 bytes

Code:

print int input "%2

Result:

Returns 0 for even numbers, and 1 for odd numbers.

Pip, 3 bytes

a%2

Try it online!

Explanation:

a   is auto-read from the command line
%2  Modulo 2 --> yields a 0 for even, 1 for odd
The result of the last statement is auto-printed.

Jellyfish, 6 bytes

p|i
2

Try it online!

Print (p) the input (i) modulo (|) two (2).

Carrot, 5 bytes

#^F%2

Try it online!

Note: this is a trivial answer. Please do not vote. I am only posting this for completion.

Explanation

#^                // set the stack to the input
F               // convert it to a float
%2             // take the modulo 2 of it
// implicit output
• That's okay. Every answer else is trivial too. At least it's not a builtin. – Matthew Roh Mar 23 '17 at 9:32
• Why do you have to convert to float? – Hello Goodbye Nov 14 at 16:27
• @HelloGoodbye Because it is by default a string, although yes, I should work just as well – Kritixi Lithos Nov 14 at 16:57
• ah, makes sense – Hello Goodbye Nov 14 at 17:05

APL (Dyalog), 3 bytes

Full program body:

2|⎕

Try it online!

Alternatively:

2⊤⎕

Try it online!

| is modulus, is base-convert, is numeric input prompt. Handles any numeric array.

I've found 40 functions which can also do the job in 3 bytes. List and try them all online!

C, 11 bytes

f(n){n&=1;}

Because, why not. Returns false (0) for even, true (1) for odd.

Works with gcc on linux/x86.

• Huh. Looks okay. – Matthew Roh Mar 23 '17 at 9:49

Haxe, 22 bytes

function(x)return x%2;

Test it online!

Haxe is a high-level, strictly typed language designed to be compiled across many different platforms. While it doesn't have any form of lambda expressions, it does have one unique property that allows for interesting golfing: everything is an expression. This allows you to do interesting things that aren't possible in most similar languages, such as b>0&&return b. In this particular case, it allows us to remove the brackets that would normally be required in a function definition.

• High level language, low level answer (lol) – Matthew Roh Mar 23 '17 at 13:37

Qwerty-RPN, 4 bytes

@2%#

Explanation:

@    Input number
%  ...modulo...
2   2
# print

Beeswax, 6 bytes

_2~,%{

Explanation:

_        Create a bee flying horizontally   [0,0,0]
2       Set top to 2                       [0,0,2]
~      Swap top and 2nd values            [0,2,0]
,     Take value from STDIN as int       [0,2,7]
%    Modulo: top % 2nd                  [0,0,1]
{   Print top                          [0,0,1]

Try it online!

Ook!, 79 64 bytes

Update: 15 bytes shorter by removing the whitespaces, thanks to Okx.

This is a joke esoteric language, meant to be trivially isomorphic to brainfuck by substituting each command with an Orangutan phrase. This is my first time using it.

Ook.Ook.Ook!Ook?Ook.Ook!Ook.Ook?Ook.Ook!Ook?Ook!Ook?Ook.Ook!Ook.

Try it here! Give input as unary. Output is 1 for odd numbers, and nothing for even ones.

Explanation:

The above script is a direct translation of the 8 bytes brainfuck answer by @Dennis:

+[,>,]<.

Ook! has only 3 distinct syntax elements: Ook., Ook? and Ook!. These are combined into groups of two, and the various pair combinations are mapped to the brainfuck commands.

Substitution table:

>   Ook.Ook?
<   Ook?Ook.
+   Ook.Ook.
-   Ook!Ook!
.   Ook!Ook.
,   Ook.Ook!
[   Ook!Ook?
]   Ook?Ook!
• You can golf it by removing the newlines. – Okx Mar 23 '17 at 14:27
• @Okx On a second look, the online interpreter from my answer works with no whitespace delimiter. Cool! That means I can remove the spaces too. Thanks for pointing that out. I've read the wiki and the Ook! creator's page and only the newlines were said to be ignored, nothing about the spaces. – seshoumara Mar 23 '17 at 15:13

Gibberish, 8 bytes

eli2gmeo

eli      - read line, convert to integer
2gm   - push 2, push modulo of previous value by 2
eo - output stack

Outout 0 for even and 1 for odd

J-uby, 6 bytes

:even?

In J-uby, Ruby's symbols are callable. Fixnum#even? in Ruby (predictably) returns whether a number is even or not. It can be called like so:

f = :even?
f.(2) #=> true
f.(3) #=> false

J, 8 chars

<&1@:|~&2

It's just a composition of

<&1

which flips 0 for 1, and

|~&2

which is mod 2.

The @: just composes the two functions together

• You don't need <&1@. It's up to us which Boolean corresponds to which parity. Also, 2&| saves a byte. Try it online! – Dennis Mar 25 '17 at 12:40
• Welcome to the site! – caird coinheringaahing Mar 31 '17 at 6:40

Elisp, 12 10 bytes

(read) evaluates to the input, and the (% ...) expression is then evaluated. Outputs 1 for odd, 0 for even.

Test cases:

(Input):(Output)
1:1
2:0
16384:0
99999999:1

Edit: Saves 2 bytes thanks to @Dylan, for asking if it was possible to leave out the spaces in a Elisp expression. Turns out the answer is yes!

Python REPL, 3 bytes

_&1

Explanation: Taking bitwise and with 1 gives True when even else False

• Just so you know, the downvote was cast automatically by the Community user when you edited your answer. I consider this a bug. – Dennis Mar 26 '17 at 15:08
• Note to voters: _ is the value of the last expression in an interactive session, so the code can be used like this. Per consensus, this is currently a valid form of taking input. – Dennis Mar 26 '17 at 16:08
• Welcome to PPCG! – Pavel Mar 26 '17 at 19:40
• @ГригорийПерельман thank you – Aashutosh Rathi Mar 28 '17 at 7:33
• @Dennis thanks for edit 👍 – Aashutosh Rathi Mar 28 '17 at 7:34

J, 2 bytes

2|         NB. remainder of arg / 2

So 0 = true (even), 1 = false (odd)
2| 7   => 1
2| 10  => 0
• This doesn't look right. I get a syntax error. I think you need another byte: 2|] or 2&| – Adám May 8 '17 at 8:55

Acc!!, 43 bytes

N
Count i while _/10 {
N-_%2*2
}
Write _+41

Because of quirks of the input mechanism in Acc!!, the input number (given on stdin) has to be terminated with a signal value--in this case, a tab character. The code outputs 2 if the number is even, 0 if it is odd.

Explanation

# Read a character's ASCII value into the accumulator
N
# Loop while accumulator intdiv 10 is greater than 0 (i.e. acc >= 10)
Count i while _/10 {
# Read another ASCII value, subtract (current acc % 2) * 2, and store back to acc
N-_%2*2
}
# At the end of the loop, we know we just read a tab character (ASCII 9). This means the
# acc value is 9 if the previous digit had an even ASCII value, or 7 if it was odd. We
# add 41 to convert to the ASCII codes of 2 and 0, respectively, and write to stdout.
Write _+41

Scala, 9 bytes

(_:Int)%2

This outputs 1 for odd, 0 for even