# 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

# ArnoldC, 299 283 bytes

IT'S SHOWTIME
HEY CHRISTMAS TREE i
YOU SET US UP 0
GET YOUR ASS TO MARS i
DO IT NOW
I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY
GET TO THE CHOPPER i
HERE IS MY INVITATION i
I LET HIM GO 2
ENOUGH TALK
TALK TO THE HAND i
YOU HAVE BEEN TERMINATED

This outputs 1 (which is truthy) for odd input and 0 (which is falsy) for even input.

Try it online!

## “Now this is the plan” (An attempt at an explanation)

The code reads the input into variable i, replaces it with the result of modulo 2, and then prints it.

IT'S SHOWTIME                                    # BeginMain
HEY CHRISTMAS TREE i                             #   Declare i
YOU SET US UP 0                                  #   SetInitialValue 0
GET YOUR ASS TO MARS i                           #   AssignVariableFromMethodCall i
DO IT NOW                                        #   CallMethod
GET TO THE CHOPPER i                             #   AssignVariable i
HERE IS MY INVITATION i                          #     SetValue i (push i on the stack)
I LET HIM GO 2                                   #     ModuloOperator 2
ENOUGH TALK                                      #   EndAssignVariable
TALK TO THE HAND i                               #   Print i
YOU HAVE BEEN TERMINATED                         # EndMain
• My first ArnoldC answer! – Luis Mendo Mar 21 '17 at 14:01
• BULLSHIT YOU HAVE NO RESPECT FOR LOGIC GET TO THE CHOPPER – Magic Octopus Urn Mar 21 '17 at 14:27
• GET YOUR ASS TO MARS ...I have no words. – Matthew Roh Mar 21 '17 at 16:00
• I joined this community to upvote this. Well done, sir – Erik Mar 22 '17 at 15:23
• I joined this community to upvote this post as well. :) – Vada Poché Mar 24 '17 at 4:52

# brainfuck, 8 bytes

+[,>,]<.

Input is in unary. Output is the 1 (truthy) for odd numbers and NUL (falsy) for even numbers.

Try it online!

### How it works

We start by incrementing the current cell with + to be able to enter the while loop [,>,].

In each iteration, , reads a byte from STDIN, > advances to the cell to the right, then , reads another byte from STDIN. When input is exhausted, the interpreter (the one on TIO, anyway) will set the cell to NUL instead. Once that happens, the condition of the while loop is no longer fulfilled and we break out of it.

Let n be the input integer. If there is an even amount of input bytes – i.e., if n is even – the first n/2 iterations will read two 1's, and the next iteration will read two NUL's, leaving the tape as follows.

...   1  NUL  NUL
...  49    0    0
^

<. retrocedes one cell and prints its content, sending a NUL byte to STDOUT.

However, if there is an odd amount of input bytes, the first (n - 1)/2 iterations will read two 1's, and the next iteration will read one 1 and one NUL, leaving the tape as follows.

...   1    1  NUL
...  49   49    0
^

< will now retrocede to a cell holding the byte/character 1, which . prints.

## Mathematica, 4 bytes

OddQ

Gives True for odd inputs and False for even inputs, who knew?

There's also EvenQ, but who would want to type all of that?

• Oh, no. builtins again. – Matthew Roh Mar 21 '17 at 10:43
• @SIGSEGV That's Mathematica for ya. ;) – Kevin Cruijssen Mar 21 '17 at 10:44
• 2∣#& works also – Kelly Lowder Mar 22 '17 at 18:47
• @KellyLowder true but that's 6 bytes. – Martin Ender Mar 22 '17 at 19:00
• Why does the name end in a Q? – Cyoce Jun 17 '17 at 7:52

# Taxi, 1,4821,2901,0631,029 1,009 bytes

I've never written a program in Taxi before and I'm a novice in programming for general, so there are probably better ways to go about this. I've checked for errors and managed to golf it a bit by trying different routes that have the same result. I welcome any and all revision.

Returns 0 for even and 1 for odd.

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Divide and Conquer.2 is waiting at Starchild Numerology.Go to Starchild Numerology:n 1 l 1 l 1 l 2 l.Pickup a passenger going to Divide and Conquer.Go to Divide and Conquer:e 1 l 2 r 3 r 2 r 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:e 1 l 1 l 2 l.Pickup a passenger going to Trunkers.Pickup a passenger going to Equal's Corner.Go to Trunkers:s 1 l.Pickup a passenger going to Equal's Corner.Go to Equal's Corner:w 1 l.Switch to plan "b" if no one is waiting.Pickup a passenger going to Knots Landing.Go to Knots Landing:n 4 r 1 r 2 r 1 l.[a]Pickup a passenger going to The Babelfishery.Go to The Babelfishery:w 1 l.Pickup a passenger going to Post Office.Go to Post Office:n 1 l 1 r.[b]0 is waiting at Starchild Numerology.Go to Starchild Numerology:n 1 r.Pickup a passenger going to Knots Landing.Go to Knots Landing:w 1 r 2 r 1 r 2 l 5 r.Switch to plan "a".

Try it online!

You're right, that's awful to read without line breaks. Here's a formatted version:

Go to Post Office:w 1 l 1 r 1 l.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:s 1 l 1 r.
Pickup a passenger going to Divide and Conquer.
2 is waiting at Starchild Numerology.
Go to Starchild Numerology:n 1 l 1 l 1 l 2 l.
Pickup a passenger going to Divide and Conquer.
Go to Divide and Conquer:e 1 l 2 r 3 r 2 r 1 r.
Pickup a passenger going to Cyclone.
Go to Cyclone:e 1 l 1 l 2 l.
Pickup a passenger going to Trunkers.
Pickup a passenger going to Equal's Corner.
Go to Trunkers:s 1 l.
Pickup a passenger going to Equal's Corner.
Go to Equal's Corner:w 1 l.
Switch to plan "b" if no one is waiting.
Pickup a passenger going to Knots Landing.
Go to Knots Landing:n 4 r 1 r 2 r 1 l.
[a]Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:w 1 l.
Pickup a passenger going to Post Office.
Go to Post Office:n 1 l 1 r.
[b]0 is waiting at Starchild Numerology.
Go to Starchild Numerology:n 1 r.
Pickup a passenger going to Knots Landing.
Go to Knots Landing:w 1 r 2 r 1 r 2 l 5 r.
Switch to plan "a".

Here's my best attempt to explain the logic:

Go to Post Office to pick up the stdin value in a string format.
Go to The Babelfishery to convert the string to a number.
Go to Starchild Numerology to pickup the numerical input 2.
Go to Divide and Conquer to divide the two passengers (stdin & 2).
Go to Cyclone to create a copy of the result.
Go to Trunkers to truncate the original to an integer.
Go to Equal's Corner to see if the two passengers are the same.
Equal's Corner returns the first passenger if they're the same (no .5 removal so the stdin was even) or nothing if they're not.
If nothing was returned, it was odd, so go pick up a 0 from Starchild Numerology.
Go to Knots Landing to convert any 0s to 1s and all other numbers to 0s.
Go to The Babelfishery to convert the passenger (either a 1 or 0 at this point) to a string.
Go to Post Office to print that string.
Try and fail to go to Starchild Numerology because the directions are wrong so the program terminates.

Not going back to the Taxi Garage causes output to STDERR but I'm fine with that.

• I always learned that goto is evil – aross Mar 22 '17 at 13:37
• Not only does the language require the extensive use of go to, the only branching method is by using plans, which are just a different name for goto. – Engineer Toast Mar 22 '17 at 13:51

## Retina, 8 bytes

[02468]$A Retina answer for decimal input. This is also a plain regex solution that works in almost any regex flavour. Matches (and prints 1) for even inputs and doesn't match (and prints 0) for odd inputs. Try it online! An alternative, also for 8 bytes, uses a transliteration stage to turn all even digits to x first (because transliteration stages have a built-in for even/odd digits): TEx x$

Input given as a command-line parameter to this function f. Output returned as a shell return value - display with echo $? # Bash + bc, 211411 9 bytes bc<<<$1%2

Reads command-line input, expands the value into the string with the mod operation, and pipes the string to bc for calculation. Outputs 1 for odd, 0 for even.

Test cases:

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

Edit: saved 7 bytes thanks to @ais523
Edit 2: saved another 3 bytes thanks to @Dennis
Edit 3: saved another two thanks to @Dennis

• Welcome to the site! – James Mar 24 '17 at 17:52
• Perhaps you could take input from a command-line argument to bash (such as $1) rather than spending bytes reading it from stdin? – user62131 Mar 26 '17 at 3:25 • @ais523: Great suggestion! I should have thought about doing it in a script instead of just on the command line. – Christopher Pitts Mar 26 '17 at 4:04 • You can shorten this to bc<<<$1%2. – Dennis Mar 26 '17 at 14:38
• You don't need the quotes. Neither digits nor % require them. – Dennis Mar 27 '17 at 3:17

## Alice, 7 bytes

1\@
oAi

Try it online!

Prints 0 for even inputs and 1 for odd inputs.

### Explanation

This is structurally similar to my addition program, but the flipped mirror subtly changes the control flow:

1    Push 1.
\    Reflect northeast. Switch to Ordinal.
Bounce off the boundary, move southeast.
i    Read all input as a string.
Bounce off the corner, move back northwest.
\    Reflect south. Switch to Cardinal.
A    Implicitly convert input string to integer. Compute bitwise AND with 1.
Wrap around to the first line.
\    Reflect northwes. Switch to Ordinal.
Bounce off the boundary, move southwest.
o    Implicitly convert result to string and print it.
Bounce off the corner, move back northeast.
\    Reflect east. Switch to Cardinal.
@    Terminate the program.