Is this even or odd?

67

6

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

Leaderboard

var QUESTION_ID=113448,OVERRIDE_USER=64499;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){var F=function(a){return a.lang.replace(/<\/?a.*?>/g,"").toLowerCase()},el=F(e),sl=F(s);return el>sl?1:el<sl?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

Matthew Roh

Posted 2017-03-21T10:30:34.823

Reputation: 5 043

2This isn't the first time I've confused mathematical with computational parity... this is a code site after all! – Neil – 2017-03-21T10:38:25.247

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 – 2017-03-21T13:57:49.687

@mbomb007 Why the change from Catalogue to Leaderboard? – Mike Bufardeci – 2017-03-21T16:36:18.227

4@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 – 2017-03-21T16:37:11.000

is 0 an even number? – tuskiomi – 2017-03-22T00:12:25.613

2@tuskiomi The challenge only asks about positive integers. (0 is considered even but not positive) – Calvin's Hobbies – 2017-03-22T02:46:00.537

I'm new here. Just curious, just subscribed, but I do not even understand what's being asked here... any help understanding? – Lucio Crusca – 2017-03-23T14:49:44.197

3

@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 – 2017-03-23T15:09:31.057

As @HelkaHomba mentioned, zero is not positive; what is the expected result for input of zero? Or do we not care? – Richard – 2017-03-26T18:42:19.257

Could we just return the least significant bit? – SIGSTACKFAULT – 2017-03-26T18:53:15.443

@Richard We dont care. – Matthew Roh – 2017-03-26T22:48:43.437

@Blacksilver Yes, nearly one third of the answer already does that (&1) – Matthew Roh – 2017-03-26T22:49:33.443

@SIGSEGV I think the snippet is broken. – OldBunny2800 – 2017-03-29T20:22:10.040

@OldBunny2800 Yeah, I thought so too, but it is not a thing I can handle. – Matthew Roh – 2017-03-29T22:58:06.073

@You (that sounds so funny) can I just use this Pyt Program and expect the last digit to be the truthy/falsey value? (0 for even and 1 for odd)

– FantaC – 2018-02-01T05:01:54.073

Answers

117

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
I WANT TO ASK YOU ... ANSWERED IMMEDIATELY       #   ReadInteger
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

Luis Mendo

Posted 2017-03-21T10:30:34.823

Reputation: 87 464

28My first ArnoldC answer! – Luis Mendo – 2017-03-21T14:01:35.533

17BULLSHIT YOU HAVE NO RESPECT FOR LOGIC GET TO THE CHOPPER – Magic Octopus Urn – 2017-03-21T14:27:29.613

19GET YOUR ASS TO MARS ...I have no words. – Matthew Roh – 2017-03-21T16:00:43.450

13I joined this community to upvote this. Well done, sir – Erik – 2017-03-22T15:23:24.050

1@Erik Haha. Thanks, and welcome to PPCG! – Luis Mendo – 2017-03-22T15:46:36.790

I'm confused at what the DO IT NOW does here – Cruncher – 2017-03-23T17:46:57.203

I'd pay (well, upvote) for an explanation of the code ^^ – Olivier Dulac – 2017-03-23T17:48:55.620

1@OlivierDulac I haven't watched enough Schwarzenegger movies to be fluent with this language, but I just posted an attempt at an explanation – Luis Mendo – 2017-03-23T18:18:12.460

1@LuisMendo: I thank (and upvote) thee! – Olivier Dulac – 2017-03-23T18:24:38.047

2I joined this community to upvote this post as well. :) – Vada Poché – 2017-03-24T04:52:39.527

When you say SetValue i in the AssignVariable bit, do you mean GetValue? – Fund Monica's Lawsuit – 2017-03-27T02:00:37.513

@QPaysTaxes HERE IS MY INVITATION sets a value on the top of the stack. I've clarified a bit. See the pattern here

– Luis Mendo – 2017-03-27T09:48:29.610

49

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.

Dennis

Posted 2017-03-21T10:30:34.823

Reputation: 196 637

37

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?

Martin Ender

Posted 2017-03-21T10:30:34.823

Reputation: 184 808

22Oh, no. builtins again. – Matthew Roh – 2017-03-21T10:43:43.913

8@SIGSEGV That's Mathematica for ya. ;) – Kevin Cruijssen – 2017-03-21T10:44:15.063

72∣#& works also – Kelly Lowder – 2017-03-22T18:47:31.140

1@KellyLowder true but that's 6 bytes. – Martin Ender – 2017-03-22T19:00:51.427

Why does the name end in a Q? – Cyoce – 2017-06-17T07:52:14.327

@Cyoce most predicates do, like IntegerQ or SortedQ. I suppose it stands for "question" or "query", but I don't know. – Martin Ender – 2017-06-17T07:54:34.610

So it's like ? in Ruby? – Cyoce – 2017-06-17T08:05:49.253

@Cyoce yeah pretty much. – Martin Ender – 2017-06-17T08:13:29.803

@MatthewRoh yes, Mathematica has built-ins for everything. If I recall, there was some discussion about the built in function for determining if a given image contained animals, or some such. – Brian Minton – 2019-11-14T14:32:33.673

27

Taxi, 1,482 1,290 1,063 1,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.

Engineer Toast

Posted 2017-03-21T10:30:34.823

Reputation: 5 769

8I always learned that goto is evil – aross – 2017-03-22T13:37:58.003

2Not 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 – 2017-03-22T13:51:01.123

24

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):

T`E`x
x$

Of course, the shortest input format (even shorter than unary) would be binary in this case, where a simple regex of 0$ would suffice. But since the challenge is essentially about finding the least-signficant binary digit, binary input seems to circumvent the actual challenge.

Martin Ender

Posted 2017-03-21T10:30:34.823

Reputation: 184 808

1+1 for "TeX". Makes me want to see a LaTeX answer... – Brevan Ellefsen – 2017-03-22T18:19:39.943

23

LOLCODE, 67 bytes

HOW DUZ I C YR N
  VISIBLE BOTH SAEM MOD OF N AN 2 AN 0
IF U SAY SO

Function that returns WIN (true) if number is even, else (odd) it will return FAIL (false).

Call with C"123".

devRicher

Posted 2017-03-21T10:30:34.823

Reputation: 1 609

21

Python, 11 10 bytes

-1 byte thanks to Griffin

1 .__and__

Try it online!
Using bitwise and, returns 0 for even and 1 for odd

Rod

Posted 2017-03-21T10:30:34.823

Reputation: 17 588

31 .__and__ is one char shorter – Griffin – 2017-03-21T12:28:27.850

Such an awesome abuse of the language! – Brian Minton – 2019-11-14T14:45:03.147

20

MATL, 5 3 bytes

Because builtins are boring

:He

This outputs a matrix of nonzero values (which is truthy) for even input, and a matrix with a zero in its lower right entry (which is falsy) for odd input.

Try it online! The footer code is an if-else branch to illustrate the truthiness or falsihood of the result. Removing that footer will implicitly display the matrix.

Explanation

Consider input 5 as an example

:     % Implicitly input n. Push row vector [1 2 ... n]
      % STACK: [1 2 3 4 5]
He    % Reshape into a 2-row matrix, padding with zeros if needed
      % STACK: [1 3 5;
                2 4 0]

Luis Mendo

Posted 2017-03-21T10:30:34.823

Reputation: 87 464

7'Builtins are boring' HeAA, HeAA, HeAA.(Sorry that was a bad pun) – Matthew Roh – 2017-03-21T11:22:01.513

3@SIGSEGV HeHeHe – Luis Mendo – 2017-03-21T11:28:37.840

2Clever approach! :) – Stewie Griffin – 2017-03-21T11:37:53.740

18

Java 8, 8 bytes

n->n%2<1

Try it here.

Java 7, 30 bytes

Object c(int n){return n%2<1;}

Try it here.

Outputs true for even numbers and false for odd numbers


If 1/0 would be allowed instead of true/false (it isn't, considering the numbers of votes here):

  • Java 8 (6 bytes): n->n%2
  • Java 7 (25 bytes): int c(int n){return n%2;}

Kevin Cruijssen

Posted 2017-03-21T10:30:34.823

Reputation: 67 575

24Where is all the ridiculously verbose Java-stuff? I feel this is at least 50 bytes too short... – Stewie Griffin – 2017-03-21T12:10:02.570

@StewieGriffin Hehe, yeah, Java 8 turns out to be pretty short for some challenges. :) 8 bytes for this challenge, actually competing with the rest (except for the 1 / 2 byte answers from code-golf languages). Yesterday I posted this answer, which is also competing with it's 19 bytes. Who knows, perhaps Java will ever win one (Not!.. ;p). (PS: Makes me wonder: Is there a single challenge on this SE with a Java answer as accepted answer? xD Definitely not for code-golf, but perhaps code-challenge or something similar.)

– Kevin Cruijssen – 2017-03-21T12:14:02.480

2

Once upon a time there was a tag called code-trolling. But this one is a very nice accepted Java-answer, and here is a code-golf answer. And some more.

– Stewie Griffin – 2017-03-21T12:20:57.593

@StewieGriffin Ah yes, I've seen those answers before. (About a year ago when I was sorting all questions/answers by vote and was looking at all of them one by one.) Too bad code-trolling isn't a tag anymore, looks like a lot of fun to create something as verbose and useless as possible for something that can usually be done with a single builtin. – Kevin Cruijssen – 2017-03-21T12:37:54.563

@Kevin Where can i find golfing rules for Java? I've been lurking at this site for some time and have always been astounded with clevereness of many solutions. Yet, I have always been wondering why the usual boilerplate code is not included in the byte count (i.e. no public static void main shenanigans in case of Java). The code above won't compile w/o standard Java class declarations and input reading routine needs to be added as well (Java 9 REPL may change it). Is the site consensus that standard boilerplate code is not accounted for when measuring programs' length? Thanks for clarifying! – lukeg – 2017-03-23T14:25:11.300

2

@lukeg Hi, the default is program or function, unless the challenge states otherwise. Which means languages like Java and C# are allowed to post just the function (and required imports) instead of the entire class. If the question-asker would specifically ask for a program, then I indeed have to include the borderline code like the class/interface and main-method.

– Kevin Cruijssen – 2017-03-24T07:50:08.567

1

@lukeg If you want to start answering challenges yourself, here are some tips for golfing in Java which might be interesting to read through. Welcome to PPCG! :)

– Kevin Cruijssen – 2017-03-24T07:50:49.720

6@StewieGriffin There you go! int o(int n){return java.util.stream.IntStream.of(n).map(n->n%2).filter(n==0).fi‌​ndAny().isPresent();‌​} – Olivier Grégoire – 2017-03-24T12:31:06.630

Is taking little-endian (least significant digit first) input acceptable? – SuperJedi224 – 2019-03-27T22:10:31.933

@SuperJedi224 Since a positive integer is being asked, probably not. Not sure if there are any meta rules for that, but usually when an integer-input is being asked, only an integer/String is allowed by default (and list of digits on request of the challenge poster). For some languages that don't have integers, like Regex, a unary input is allowed as alternative. But whether a little-endian input is allowed when an integer input is being asked, I kinda doubt. But feel free to see if a meta-post for it exists, and if not, create one to ask if it's allowed. :) – Kevin Cruijssen – 2019-03-28T07:43:54.520

This answer and the "if 1 and 0" are allowed answer are correct for Julia as well. – gggg – 2019-11-14T17:20:12.463

17

Piet, 15 codels / 16 bytes

Source Code

5njaampjhompppam

Online interpreter available here.

This program returns 0 if the input is even and 1 if the input is odd.

The text above represents the image. You can generate the image by pasting it into the text box on the interpreter page. For convenience I have provided the image below where the codel size is 31 pixels. The grid is there for readability and is not a part of the program.

Explanation

This program uses the modulo builtin to determine if the input is even or odd.

Instruction    Δ Hue   Δ Lightness   Stack
------------   -----   -----------   -------
In (Number)    4       2             n
Push [2]       0       1             2, n
Modulo         2       1             n % 2
Out (Number)   5       1             [Empty]
[Exit]         [N/A]   [N/A]         [Empty]

The dark blue codels in the bottom-left are never visited and can be changed to any color other than a color of a neighboring codel. I chose dark blue as I think it looks nice with the rest of the program. The top-left black codel could also be white, but not any other color. I have chosen black as I think it looks nicer.

I have provided the program in both image form and text form as there is no clear consensus on how to score Piet programs. Feel free to weigh in on the meta discussion.

Mike Bufardeci

Posted 2017-03-21T10:30:34.823

Reputation: 1 680

15

brainfuck, 14 bytes

Input and output is taken as character codes as per this meta.
Byte value 1 correspond to odd numbers and 0 to even.

+>>,[-[->]<]<.

Try it online!

Emigna

Posted 2017-03-21T10:30:34.823

Reputation: 50 798

15

JavaScript, 6 bytes

An anonymous function:

n=>n&1

Alternatively with the same length:

n=>n%2

Both will return 0|1 which should fulfill the requirement for truthy|falsey values.

Try both versions online

insertusernamehere

Posted 2017-03-21T10:30:34.823

Reputation: 4 551

According to the Java answer this doesn't fulfil the requirements. Is JavaScript different in this regard?

– TheLethalCoder – 2017-03-21T11:33:53.437

5The question clearly states "This is not required, You may use other Truthy/Falsy values", which 0|1 are, right? @TheLethalCoder – insertusernamehere – 2017-03-21T11:35:51.240

1

I'm not sure if they are in JavaScript that's what I was asking, see the meta q/a to see if they are. I'm not familiar enough in JavaScript to know.

– TheLethalCoder – 2017-03-21T11:37:40.983

5JavaScript is much more laxly typed than Java. It's happy to treat pretty much anything as any type. In particular, it's happy to treat floats as booleans (whereas Java will throw a compile-time error if you do this). (By the way, you possibly don't want to know why this returns a float rather than an integer.) – None – 2017-03-21T11:38:38.187

@TheLethalCoder Ah, thanks for the meta post. It makes the answer clear: Yes, they are valid. – insertusernamehere – 2017-03-21T11:40:08.733

1Good :) I wasn't familiar enough to know myself so was just wondering. – TheLethalCoder – 2017-03-21T11:42:32.293

13

Japt, 1 byte

v

Returns 1 for even numbers, 0 for odd.

Try it online!

Explanation

One of Japt's defining features is that unlike most golfing languages, functions do not have fixed arity; that is, any function can accept any number of arguments. This means that you can sometimes leave out arguments and Japt will guess what you want. v on numbers is a function that accepts one argument and returns 1 if the number is divisible by the argument, else 0. For example:

v3

This program will output 1 if the input is divisible by 3, and 0 otherwise. It just so happens that the default argument is 2, thereby solving this challenge in a single byte.


Alternative 1 byte solution:

¢

¢ converts the input into a base-2 string. The -h flag returns the last char from the string.

Try it online!

Oliver

Posted 2017-03-21T10:30:34.823

Reputation: 7 160

12

Sinclair ZX81 BASIC 124 bytes 114 bytes 109 bytes 57 50 42 tokenized BASIC bytes

Another release candidate:

 1 INPUT A
 2 IF A THEN PRINT A;":";NOT INT A-(INT (INT A/VAL "2")*VAL "2")

As per Adám's comments below:

 1 INPUT A
 2 IF NOT A THEN STOP
 3 PRINT A;":";NOT INT A-(INT (INT A/VAL "2")*VAL "2")

It will now PRINT 1 for even and 0 for odd. Zero exits.

Here are older versions of the symbolic listing for reference purposes:

 1 INPUT A
 2 IF NOT A THEN STOP
 3 LET B=INT (INT A/2)
 4 PRINT A;":";NOT INT A-B*2
 5 RUN

Here is the old (v0.01) listing so that you may see the improvements that I've made as not only is this new listing smaller, but it's faster:

 1 INPUT A
 2 IF A<1 THEN STOP
 3 LET B=INT (INT A/2)
 4 LET M=1+INT A-B*2
 5 PRINT A;":";
 6 GOSUB M*10
 7 RUN
10 PRINT "TRUE"
11 RETURN
20 PRINT "FALSE"
21 RETURN

And here is v0.02 (using Sinclair sub strings):

 1 INPUT A
 2 IF NOT A THEN STOP
 3 LET B=INT (INT A/2)
 4 LET M=1+INT A-B*2
 5 LET C=4*(M=2)
 6 PRINT A;":";"TRUE FALSE"(M+C TO 5+C+(M=2))
 7 RUN

ZX81 in action - true or false from v1/2

Shaun Bebbers

Posted 2017-03-21T10:30:34.823

Reputation: 1 814

1This is not [tag:kolmogorov-complexity]. You just have to return 0 or 1 for any given input. – Adám – 2017-03-23T10:04:23.717

The question looks like it was edited since I made my initial entry as it <i>read</i> like true/false required to be returned. As such, I can simplify the symbolic listing further. – Shaun Bebbers – 2017-03-23T10:14:29.127

1Why do you need to exit on zero? Can't you just PRINT (A-2*INT A/2)/A? – Adám – 2017-03-24T11:43:55.873

In the original question, which has been helpfully edited, it specified that the value 0 should not produce a TRUE or FALSE, hence as 0 wasn't supposed to produce a result then I had it STOPping the program. I probably interpreted the original question as it was posted by @SIGSEGV a little too literally. Yes, one can optimize and refactor, you are correct. – Shaun Bebbers – 2017-03-24T12:02:05.587

11

brainfuck, 12 bytes

,++[>++]>++.

This requires an interpreter with a circular tape and cells that wrap around. The one on TIO has 65,536 8-bit cells and satisfies the requirements.

I/O is in bytes. Odd inputs map to 0x00 (falsy), even inputs to a non-zero byte (truthy).

Try it online!

How it works

We start by reading a byte of input with , and adding 2 to its value with ++. We'll see later why incrementing is necessary.

Next, we enter a loop that advances to the cell at the right, add 2 to it, and repeats the process unless this set the value of the cell to 0.

Initially, all cells except for the input cell hold 0. If the input is odd, adding 2 to it will never zero it out. However, after looping around the tape 127 times, the next iteration of the loop will set the cell to the right of the input cell to 128 × 2 = 0 (mod 256), causing the loop to end. >++ repeats the loop body one more time, so next cell is also zeroed out and then printed with ..

On the other hand, if the input is n and n is even, the code before the loop sets the input cell to n + 2. After looping around the tape (256 - (n - 2)) / 2 = (254 - n) / 2 times, the input cell will reach 0, and the cell to its right will hold the value (254 - n) / 2 × 2 = 254 - n. After adding 2 with >++, . will print 256 - n = -n (mod 256), which is non-zero since n is non-zero.

Finally, note that the second case would print 258 - n = 2 - n (mod n) if we didn't increment the input before the loop, since one more loop around the tape would be required to zero out the input cell. The program would thus fail for input 2.

Dennis

Posted 2017-03-21T10:30:34.823

Reputation: 196 637

10

05AB1E, 1 byte

È

Fairly self-explantory. Returns a % 2 == 0

Try it online!

Okx

Posted 2017-03-21T10:30:34.823

Reputation: 15 025

É is a better choice, in my personal opinion; kidding. – Magic Octopus Urn – 2017-03-21T14:17:01.697

7I assume É returns a % 2 == 1. – SIGSTACKFAULT – 2017-03-26T19:07:52.813

2It's only self-explanatory if you already know the operator – MilkyWay90 – 2019-02-12T00:15:31.900

10

BitCycle, 19 17 16 15 bytes

?ABv
 // <
!+\<

Try it online!

Argh, I feel like there's a 18 byte solution floating just out of reach :( Haha! -2 bytes by using a + to redirect bits coming from different directions.

Explanation:

?ABv   Feed unary input into the main loop
 /     Every loop, two bits will be removed from the input
 +\<   By the \ and /s each
       When we reach the point where there is either one or no bits of input left
 // <  If one, it will reflect off all three /\s and turn west at the +
!+\    And then output, otherwise the program ends since no more bits are moving

Jo King

Posted 2017-03-21T10:30:34.823

Reputation: 38 234

9

Retina, 3 bytes

11

The trailing newline is significant. Takes input in unary. Outputs 1 for odd numbers, nothing for even numbers. Try it online!

Neil

Posted 2017-03-21T10:30:34.823

Reputation: 95 035

You know, you can just copy the full answer (with the necessary formatting for the trailing linefeed) from TIO. – Martin Ender – 2017-03-21T10:50:53.150

@MartinEnder No, I didn't know. – Neil – 2017-03-21T10:52:06.937

It's the second-to-last snippet when you generate the permalink. – Martin Ender – 2017-03-21T10:54:14.583

Oh, it's a whole snippet? I just see the title line. – Neil – 2017-03-21T10:59:50.513

@Neil if you click in the snippet, it will expand it and you'll see the body – Dada – 2017-03-21T11:01:20.707

@Dada That's... surprising; there's no indication that it might do that. – Neil – 2017-03-21T11:03:48.470

9

Pyth, 3 2 bytes

I did it. I golfed the ungolfable. For once, it's a non-trivial solution that managed to get that last byte!

!F

Truthy on even values (not including 0, but that's not positive so...).

Explanation:

!    Not
 FQQ Applied to the input (first Q) Q times

For example, !!2 = !0 = 1, and !!!3 = !!0 = !1 = 0

I'll keep my library of 3-byte solutions here below.

"There's another answer with multiple 3-byte solutions, but it's far from complete. Let's add a couple more:

@U2

Indexes into the list [0,1] modularly, giving truthy values on odd input.

}2P

Is 2 in the prime factorization of the input? (Truthy on even)

ti2

Is the GCD of 2 and the input 2? (Truthy on even)

gx1

does XOR-ing the input with 1 not decrease it? (Truthy on even)

q_F

Basically Q == Q*-1^Q where Q is the input, but done through a loop. (Truthy on even)

_FI

Same as above.

g^_

Translates to Q <= -Q^Q (Truthy on even)

Note that any of the above solutions involving g will work with < for flipped truthy-falsiness.)

Steven H.

Posted 2017-03-21T10:30:34.823

Reputation: 2 841

1Yes, that's rather clever :) – Digital Trauma – 2017-03-26T05:05:58.403

8

TIS-100, 39 bytes

Of course, this is, more precisely, a program for the T21 Basic Execution Node architecture, as emulated by the TIS-100 emulator.

I'll refer you to this answer for a fantastically in-depth explanation of the scoring for TIS-100 programs, as well as their structure.

@0
ADD UP
G:SUB 2
JGZ G
MOV ACC ANY

Explanation:

@0          # Indicates that this is node 0
ADD UP      # Gets input and adds it to ACC, the only addressable register in a T-21
G:          # Defines a label called "G"
SUB 2       # Subtracts 2 from ACC
JGZ G       # If ACC is greater than 0, jumps to G
MOV ACC ANY # Sends the value of ACC to the first available neighbor; in this case, output.
            # Implicitly jumps back to the first line

In pseudocode, it'd look something like:

while (true) {
    acc = getIntInput()
    do {
        acc -= 2
    } while (acc > 0)
    print(acc)
}

The T21 doesn't have boolean types or truthy/falsy values, so the program returns -1 for odd numbers and 0 for even numbers, unless the previous input was odd, in which case it returns -1 for even numbers and 0 for odd numbers - if that fact disturbs you, this is a full-program answer, so you can just restart your T21 between uses.

Tutleman

Posted 2017-03-21T10:30:34.823

Reputation: 571

I was just thinking of TIS-100, the Zachtronics puzzle game, since I wanted to buy it last week. Is TIS an actual language also, or does it exist only in that video game? – seshoumara – 2017-03-21T17:16:33.493

@seshoumara To my knowledge, it only exists within the game. The entire architecture of machines in TIS is kinda typical, and this Assembly-style language hooks into that. – steenbergh – 2017-03-21T18:47:49.133

I can confirm, it only exists in the game (and in fact, even in-universe it's a weird and bizarre architecture). I wrote the answer Turtleman linked to as if there were real TIS devices, but I only did it in character for fun. – undergroundmonorail – 2017-03-23T00:06:02.617

If you don't mind, I'm going to make a SHENZHEN I/O answer now. – SIGSTACKFAULT – 2017-03-26T19:10:07.553

2@Blacksilver The real challenge, I think, would be to make a Spacechem answer! – Tutleman – 2017-03-29T06:35:36.600

Cracks knuckles – SIGSTACKFAULT – 2017-03-29T15:52:04.960

1

I have implemented a TIS emulator for TIO, so you can now try it online!

– Phlarx – 2018-05-02T20:03:55.987

can you in&out from UP? – l4m2 – 2018-11-16T08:26:46.700

8

C++, 25 bytes

template<int v>int o=v&1;

This defines a variable template (a function-like construct) with value equal to the bitwise operation input&1. 0 for even values, 1 for odd values. The value is calculated on compile-time.

Requires C++14.

Try it online!

Cássio Renan

Posted 2017-03-21T10:30:34.823

Reputation: 211

Woah, that seems really clever. I've never seen an answer like this before! How do you call this? – James – 2017-03-21T18:16:24.850

@DJMcMayhem It's a simple use of C++14's variable templates. It's not all that clever, though: A simple function aproach (int o(int v){return v&1;}) would take the same amount of bytes, with the difference that the value would be calculated at runtime.

– Cássio Renan – 2017-03-21T21:03:37.460

I don't think this actually qualifies, as the code would just be compiled into a return 1 or return 0. Running the same compiled code again would never yield a different result, it's not a function in this way. Closer to a constant. – Drunken Code Monkey – 2017-03-26T07:03:14.143

@DrunkenCodeMonkey the evaluation time is irrelevant. What matters is that I can pass arguments to the construct (provide input), and it will give back results (return output). From your point of view, no function would be qualifiable, as without a main()-like construct, the program would also just be compiled to a return 0, or even fail to compile at all. This contradicts the meta post I linked to in this answer. – Cássio Renan – 2017-03-27T16:28:08.003

2

C++ lambda function saves 3 bytes [](int x){return x%2;} Try it online

– Johan du Toit – 2017-05-17T07:40:36.833

8

Intel 8080 machine code, 1 byte

1F      RAR     ; rotate accumulator's least significant bit into Carry Flag

Input is in A, result is CF=1 if odd, CF=0 if even.

Here is a test program for the MITS Altair 8800 that will take the input number and display the result (odd or even) on the front panel lights. Just follow these programming instructions on the front panel switches:

Step    Switches 0-7    Control Switch  Instruction Comment
1                       RESET
2       00 111 110      DEPOSIT         MVI  A, 27  Load number to examine into A
3       00 011 011      DEPOSIT NEXT                Set value to test (27 in this case)
4       00 011 111      DEPOSIT NEXT    RAR         Rotate A's LSB into CF
5       11 110 101      DEPOSIT NEXT    PUSH PSW    Push Status Flags to stack
6       11 000 001      DEPOSIT NEXT    POP  B      Pop Status Flags to BC register
7       00 111 110      DEPOSIT NEXT    MVI  A, 1   CF is LSB (0x1) of Status Flags
8       00 000 001      DEPOSIT NEXT
9       10 100 001      DEPOSIT NEXT    ANA  C      Mask CF so is only result displayed
10      11 010 011      DEPOSIT NEXT    OUT  255    display contents of A on data panel
11      11 111 111      DEPOSIT NEXT
12      01 110 110      DEPOSIT NEXT    HLT         Halt CPU
13                      RESET                       Reset program counter to beginning
14                      RUN
15                      STOP
                                                    D0 light will be on if value is odd,
                                                    or off if even

If entered correctly, the program in RAM will look like: 0000 3e 1b 1f f5 c1 3e 01 a1 d3 ff 76

To re-run with a another number:

Step    Switches 0-7    Control Switch      Comment
1                       RESET
2       00 000 001      EXAMINE             Select memory address containing value
3       00 000 010      DEPOSIT             Enter new value in binary (2 in this case)
4                       RESET
5                       RUN
6                       STOP

Try it online! Just follow the easy steps above!

Output

Input = 27, light D0 is ON indicating ODD result:

enter image description here

Input = 2, light D0 is OFF indicating EVEN result:

enter image description here

640KB

Posted 2017-03-21T10:30:34.823

Reputation: 7 149

7

C#, 8 bytes

n=>n%2<1

Compiles to a Func<int, bool>.

Or if an anonymous function is disallowed, this method for 21 bytes:

bool p(int n)=>n%2<1;

TheLethalCoder

Posted 2017-03-21T10:30:34.823

Reputation: 6 930

@obarakon Not in C#, see this answer on meta. Basically if (1) does not compile.

– TheLethalCoder – 2017-03-23T09:01:58.143

there is no if (1) in your code? – YOU – 2017-03-24T07:32:31.270

1@YOU Correct, read the meta post to understand what I meant by that. – TheLethalCoder – 2017-03-24T08:50:45.803

3@YOU, the accepted definition (on this site) of a truthy/falsey value is: If if (x) evaluates to true then x is a truthy value. If it evaluates to false then it's false. So, in pseudo code: if x, disp(true), else disp(false). If that fails to compile, then x can't be used. In MATLAB and several other languages, anything that non-zero is considered true, while 0 and false are considered false. So the string Hello is a truthy value in MATLAB. However, some languages requires the value to be a boolean value (the case here), thus it must be converted to a boolean, using <1. – Stewie Griffin – 2017-03-24T08:53:42.980

I see. thanks for the explanations. – YOU – 2017-03-24T11:50:51.203

7

7, 18 characters, 7 bytes

177407770236713353

Try it online!

7 doesn't have anything resembling a normal if statement, and has more than one idiomatic way to represent a boolean. As such, it's hard to know what counts as truthy and falsey, but this program uses 1 for odd and the null string for even (the truthy and falsey values for Perl, in which the 7 interpreter is written). (It's easy enough to change this; the odd output is specified before the first 7, the even output is specified between the first two 7s. It might potentially need an output format change to handle other types of output, though; I used the two shortest distinct outputs here.)

7 uses a compressed octal encoding in which three bytes of source represent eight bytes of program, so 18 characters of source are represented in 7 bytes on disk.

Explanation

177407770236713353
 77  77     7       Separate the initial stack into six pieces (between the 7s)

        023         Output format string for "output integers; input one integer"
       7   6        Escape the format string, so that it's interpreted as is
             13     Suppress implicit looping
               3    Output the format string (produces input)
                5   Run the following code a number of times equal to the input:
   40                 Swap the top two stack elements, escaping the top one
                 3  Output the top stack element

Like many output formats, "output integers" undoes any number of levels of escaping before outputting; thus 40, which combined make a swap-and-escape operation, can be used in place of 405, a swap operation (which is a swap-and-escape followed by an unescape). If you were using an output format that isn't stable with respect to escaping, you'd need the full 405 there. (Incidentally, the reason why we needed to escape the format string originally is that if the first output contains unrepresentable characters, it automatically forces output format 7. Escaping it removes the unrepresentable characters and allows format 0 to be selected.)

Of the six initial stack elements, the topmost is the main program (and is consumed by the 13 that's the first thing to run); the second is the 023 that selects the output format and requests input, and is consumed by that operation; the third is consumed as a side effect of the 3 operation (it's used to discard stack elements in addition to producing output); the fourth, 40, is the body of the loop (and consumed by the 5 that executes the loop); and the fifth and sixth are swapped a number of times equal to the input (thus end up in their original positions if the input is even, or in each others' positions if the input is odd).

You could golf off a character by changing the leading 177 to 17 (and relying on an implicit empty sixth stack element), but that would change the parity of the outputs to a less idiomatic method than odd-is-true, and it doesn't save a whole byte (the source is still seven bytes long). As such, I decided to use the more natural form of output, as it doesn't score any worse.

user62131

Posted 2017-03-21T10:30:34.823

Reputation:

7

Brain-Flak, 22 20 bytes

Here is annother cool answer in Brain-Flak you should also check out

(({})){({}[()]<>)}<>

Try it online!

Explanation

To start we will make a copy of our input with (({})).

The bottom copy will serve as a truthy value while the top one will be used for the actual processing. This is done because we need the input to be on the top and it is rather cumbersome (two extra bytes!) to put a 1 underneath the input.

Then we begin a loop {({}[()]<>)}. This is a simple modification on the standard countdown loop that switches stacks each time it decrements.

Since there are two stacks an even number will end up on the top of the stack it started on while an odd number will end on the opposite stack. The copied value will remain in place and thus will act as a marker of where we started.

Once we are done with the loop we have a 0 (originally the input) sitting on top of either a truthy (the copy of the input) or falsy (empty stack) value. We also have the opposite value on the other stack.

We need to get rid of the 0 which can be removed either by {} or <>. Both seem to work and give opposite results, however {} causes a falsy value for zero, when it should return truthy. This is because our "truthy" value is a copy of the input and zero is the only input that can be falsy.

This problem is resolved by ending the program with <> instead.

(Of course according to the specification I do not technically have to support zero but give two options I would prefer to support it)

Post Rock Garf Hunter

Posted 2017-03-21T10:30:34.823

Reputation: 55 382

7

Pyth, 3

I was expecting pyth to have a 1 or 2 byte builtin for this. Instead here are the best solutions I could find:

%Q2

or

.&1

or

e.B

Digital Trauma

Posted 2017-03-21T10:30:34.823

Reputation: 64 644

2

Not a builtin, but there is a 2-byte solution.

– Steven H. – 2017-03-26T03:32:17.370

6

Jelly, 1 byte

Try it online!

Just another builtin.

For people who don't know Jelly: it has quite a bit of ability to infer missing bits of code, thus there isn't much syntactic difference between a snippet, a function, and a full program; the interpreter will automatically add code to input appropriate arguments and output the result. That's pretty handy when dealing with PPCG rules, which allow functions and programs but disallow snippets. In the TIO link, I'm treating this as a function and running it on each integer from 1 to 20 inclusive, but it works as a full program too.

Jelly, 2 bytes

&1

Try it online!

It's pretty short without the builtin, too. (This is bitwise-AND with 1.)

user62131

Posted 2017-03-21T10:30:34.823

Reputation:

2All these languages seem a bit of a cheat for these questions lol – Drunken Code Monkey – 2017-03-26T06:59:33.493

6

Pure Bash, 8

  • 3 bytes saved thanks to @Dennis
(($1%2))

Input given as a command-line parameter to this function f. Output returned as a shell return value - display with echo $?

Digital Trauma

Posted 2017-03-21T10:30:34.823

Reputation: 64 644

Here is a tio link: https://tio.run/##S0oszvj/X0NDxVDVSFPz////xiYA

– Brian Minton – 2019-11-14T15:10:02.840

6

Bash + bc, 21 14 11 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

Christopher Pitts

Posted 2017-03-21T10:30:34.823

Reputation: 91

2Welcome to the site! – James – 2017-03-24T17:52:07.720

Perhaps you could take input from a command-line argument to bash (such as $1) rather than spending bytes reading it from stdin? – None – 2017-03-26T03:25:24.217

@ais523: Great suggestion! I should have thought about doing it in a script instead of just on the command line. – Christopher Pitts – 2017-03-26T04:04:46.793

You can shorten this to bc<<<$1%2. – Dennis – 2017-03-26T14:38:51.693

@Dennis: Thanks! I tried that earlier, but couldn't get the syntax right. – Christopher Pitts – 2017-03-27T02:54:33.303

1You don't need the quotes. Neither digits nor % require them. – Dennis – 2017-03-27T03:17:34.467

@Dennis: Aah, I see. It's because it's a here string. Thanks for the tip! – Christopher Pitts – 2017-03-27T03:32:11.100

6

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.

Martin Ender

Posted 2017-03-21T10:30:34.823

Reputation: 184 808

5

Batch, 16 bytes

@cmd/cset/a%1%%2

Outputs 1 for odd, 0 for even. Alternative 16-byte version also works on negative numbers:

@cmd/cset/a"%1&1

17 bytes to output 1 for even, 0 for odd:

@cmd/cset/a"~%1&1

Neil

Posted 2017-03-21T10:30:34.823

Reputation: 95 035

Your program only echoes the MOD result, which is incorrect. The question said the output format should be"(Input):(Output)" – stevefestl – 2017-03-21T11:24:39.527

5

Labyrinth, 5 bytes

?_2%!

Prints 0 for even and 1 for odd inputs.

Try it online!

?   Read input.
_2  Push 2.
%   Modulo.
!   Print.

Now the instruction pointer hits a dead end and turns around. Upon attempting the % on an empty stack, the program exits due to division by zero.

alternatively, also 5 bytes

?#&!@

Try it online!

?   Read input.
#   Push stack depth (1).
&   Bitwise AND (extract least-significant bit).
!   Print.
@   Terminate.

Martin Ender

Posted 2017-03-21T10:30:34.823

Reputation: 184 808

Would #?#%! or _?#%! work too? – MildlyMilquetoast – 2017-03-21T17:09:43.357

@MistahFiggins yes. – Martin Ender – 2017-03-21T17:10:11.530

5

Hexagony, 7 bytes

?{2\%'!

Prints 0 for even numbers (falsy) and 1 for odd numbers (truthy).

Try it online!

Explanation

Here is the folded code:

 ? {
2 \ %
 ' !

I believe this is optimal although not unique. As usual I've run a brute force search (not an exhaustive one, but the characters I've excluded are very unlikely to be useful for this program). It did find a whole bunch of other solutions, which I haven't investigated in detail yet:

^?"2^%!
^?"2}%!
{?"2^%!
{?"2}%!
2^?\%"!
2}?\%"!
\{?2%'!
2^?")%!
2^?"1%!
2^?"2%!
2^?=^%!
2{?"}%!
2{?'=%!
2{?={%!
2}?")%!
2}?"1%!
2}?"2%!
2}?=^%!
|"?^2%!
)>"?}!%
\}2?%"!
2^=?^%!
2{'?^%!
2{=?{%!
2}=?^%!

As for the solution I've picked above:

?   Read input.
{   Move to the left memory edge.
    The IP wraps around to the left corner.
2   Set the memory edge to 2.
\   Deflect the IP southwest.
'   Move to the memory edge that points at the input and at the 2.
    The IP wraps around to the right corner.
%   Take the input modulo 2.
!   Print the result.
    The IP wraps to the top right corner.
{   Move to the input edge, which points at two empty edges.
%   Attempt to take the modulo of those, which terminates the program 
    due to the attempted division by zero.

I did look at the next program the above list, which is quite fun because it loops through the first two lines 6 times, filling an entire hexagonal ring with 2s before wrapping back around to the first and taking the modulo. At that point it actually uses the second 2 for the computation. I'm sure there are some gems in the others as well, but I don't think I'll have the time to go through them in detail.

Martin Ender

Posted 2017-03-21T10:30:34.823

Reputation: 184 808

5

Excel, 10 bytes

=MOD(A1,2)

Or:

=ISODD(A1)

For output of:

http://i.imgur.com/7dJydqc.png

Magic Octopus Urn

Posted 2017-03-21T10:30:34.823

Reputation: 19 422

1I've never seen excel in code golf... – programmer5000 – 2017-03-25T13:10:00.493

1Alternate Excel VBA version of this code, ?[A1]mod 2 ; an anonymous VBE immediates window function that takes input from [A1] and outputs to the VBE immediates window with 0 (falsey) representing even and 1 (truthy) representing odd – Taylor Scott – 2017-03-25T19:55:31.497

5

Cubix, 6 bytes

OI2%/@

This outputs 1 for odd numbers, and 0 for even. Try it here!

Explanation

First, the cube form.

  O
I 2 % /
  @

The following instructions are executed:

I2%O2@ # Explanation
I      # Take input
 2%    #   Modulo 2
   O   #   Output
    2  # Push 2 to the stack
     @ # End the program

Luke

Posted 2017-03-21T10:30:34.823

Reputation: 4 675

5

JSFuck, 9685 9384 6420 bytes

JSFuck is an esoteric and educational programming style based on the atomic parts of JavaScript. It uses only six different characters to write and execute code.

[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(![]+[])[!+[]+!+[]]][([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(+(+!+[]+(!+[]+[])[!+[]+!+[]+!+[]]+[+!+[]]+[+[]]+[+[]])+[])[!+[]+!+[]]+(!![]+[])[+[]]+(+(+!+[]+[+[]]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(+![]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(+![]+[![]]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]](!+[]+!+[]+[+!+[]])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]+!+[]]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(+[![]]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[+!+[]]+(+(!+[]+!+[]+[+!+[]]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(+![]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(+![]+[![]]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]](!+[]+!+[]+!+[]+[+!+[]])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]])()(([]+[])[([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()[+[]])[+[]]+[!+[]+!+[]])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[!+[]+!+[]]]

Outputs 1 for odd and 0 for even.

Try it online!

alert([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(![]+[])[!+[]+!+[]]][([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(+(+!+[]+(!+[]+[])[!+[]+!+[]+!+[]]+[+!+[]]+[+[]]+[+[]])+[])[!+[]+!+[]]+(!![]+[])[+[]]+(+(+!+[]+[+[]]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(+![]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(+![]+[![]]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]](!+[]+!+[]+[+!+[]])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]+!+[]]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(+[![]]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[+!+[]]+(+(!+[]+!+[]+[+!+[]]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(+![]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(+![]+[![]]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]](!+[]+!+[]+!+[]+[+!+[]])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]])()(([]+[])[([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()[+[]])[+[]]+[!+[]+!+[]])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[!+[]+!+[]]](prompt()))

powelles

Posted 2017-03-21T10:30:34.823

Reputation: 1 277

I think you can output 0/1 instead of true/false. alert(prompt()%2) seems to be 9384 chars. – ETHproductions – 2017-03-21T19:19:39.797

I golfed this down to 6497 chars. This equals the following JavaScript: []["fill"]["constructor"]("return this%2")["call"]. fill was chosen because that only costs 81 chars, the least of all the array methods. Also, you could argue that JSFuck is not a separate language, but rather a subset of JavaScript.

– Luke – 2017-03-21T19:27:58.920

@Luke I can't get that to run in the code snippet and since this is just a joke answer I'm going to stick with the alert based version unless you can help me figure out what I'm doing wrong. – powelles – 2017-03-21T19:54:36.037

@Luke Replace the space with a + to save a further 77 bytes ;-) And I personally think answering in JSF is fine; it's basically a dialect of JS. – ETHproductions – 2017-03-21T19:55:23.573

The code I pastied is like a function name. Just append the parentheses and include the argument in it. – Luke – 2017-03-21T20:13:46.010

@Luke Got it. Thanks. It took me a minute for me to wrap my head about it. I was trying to assign the function call to a variable. The variable has typeof function but throws TypeError: ... is not a function. – powelles – 2017-03-21T21:21:14.973

@ETHproductions I also incorporated your addition. Thanks as well. – powelles – 2017-03-21T21:21:22.500

Yeah, I've noticed that as well. Not sure why it doesn't work, though. – Luke – 2017-03-21T21:44:53.087

4

Octave, 12 bytes

@(x)mod(x,2)

Takes x modulus 2. Returns 0 for even numbers and 1 for odd numbers. These evaluates to false and true respectively in Octave. This works in MATLAB too.

Stewie Griffin

Posted 2017-03-21T10:30:34.823

Reputation: 43 471

4

QBasic 4.5, 16 15 bytes

INPUT a
?1AND a

One byte saved by @DLosc!

This shows a 1 for odd numbers and a 0 for even.

steenbergh

Posted 2017-03-21T10:30:34.823

Reputation: 7 772

1Save one byte by using bitwise operators: 1AND a is equivalent to a MOD 2. – DLosc – 2017-03-22T04:42:19.133

4

R, 12 10 9 bytes

scan()%%2

Outuputs 1 for TRUE or 0 for FALSE when input in even or odd (respectively)

0 is supported.

-2 bytes thanks to @plannapus
-1 byte thanks to @user2390246

Frédéric

Posted 2017-03-21T10:30:34.823

Reputation: 2 059

1If I may: !scan()%%2. – plannapus – 2017-03-21T14:26:34.590

1Don't 1 and 0 count as truthy and falsy in R? (They pass the "if" test). In which case you could just go with scan()%%2. – user2390246 – 2017-03-21T16:11:30.543

4

Haskell, 3 bytes:

odd

It's built-in. Works as expected. There is also even but of course that's one byte longer.

AlexJ136

Posted 2017-03-21T10:30:34.823

Reputation: 251

3

Python, 12 bytes

lambda x:x%2

use with

g = lambda x:x%2
g(a)

where a is the input number. Returns 1 for Odd, and 0 for Even.

penalosa

Posted 2017-03-21T10:30:34.823

Reputation: 505

3

Perl 5, 6 bytes

5 bytes of code + -p flag.

$_%=2

Try it online!

Dada

Posted 2017-03-21T10:30:34.823

Reputation: 8 279

3

Clojure, 4 bytes

odd? 

A function which checks if its argument is odd. Example usage: (odd? 3). Clojure does also have even? function.

cliffroot

Posted 2017-03-21T10:30:34.823

Reputation: 1 080

3

Ohm, 2 bytes

è

Explanation:

è: (pop()%2)==1
Ohm also has implict input and output

Roman Gräf

Posted 2017-03-21T10:30:34.823

Reputation: 2 915

3

Brain-Flak, 26 22 bytes

({}(())){({}[()]<>)}{}

Even: <nothing>
Odd: 1
Try it online!

({}            # Pick up the input
   (())        # Push 1
        )      # put the input back down

{          }   # While not 0
 ({}[()]  )    # Subtract 1 and...
        <>     # move to the other stack (bringing the input)
            {} # Pop the input (now 0)

Riley

Posted 2017-03-21T10:30:34.823

Reputation: 11 345

How about some friendly competition – Post Rock Garf Hunter – 2017-03-21T15:09:35.403

3

Sesos, 2 bytes

SASM

set numin
get
jmp, sub 2

SBIN

00000000: 1228                                              .(

Output is via exit code, zero for even and non-zero for odd. Odd inputs may take a long time.

Try it online!

How it works

This is a bit cheaty, but it complies with our rules.

The program reads a decimal integer from STDIN and keeps subtracting 2 until it reaches 0. For even inputs, this finishes in linear time and does nothing, successfully.

Programs that do not contain the set mask directive use arbitrary precision integers, so odd inputs will slowly continue to allocate memory. Once the available memory is exhausted, the program will get killed, resulting in a non-zero exit code. Don't expect this to happen anytime soon...

Alternate version, 3 bytes

SASM

set numin
get
jmp, sub 2

SBIN

00000000: 124601                                            .F.

Output is via exit code, zero for even and non-zero for odd. Stray output to STDOUT should be ignored.

Try it online!

How it works

Marginally less cheaty. The program works as before, but also prints the tape cell as a character in each iteration. This will exit with an error once the cell becomes negative.

Dennis

Posted 2017-03-21T10:30:34.823

Reputation: 196 637

3

PowerShell, 9 bytes

"$args"%2

Try it online!

Ho-hum. Takes input $args and stringifies it, because there can be an implicit cast from string to int, but not from array to int. Then runs that through modulo 2. Outputs 0 (a falsey value) for even and 1 (a truthy value) for odd.

AdmBorkBork

Posted 2017-03-21T10:30:34.823

Reputation: 41 581

1$arghhh, I scrolled through all the answers, didn't see PowerShell, then opened TIO and put this exact answer in. When I ran it and saw a cache hit, I was like "uh oh" and then found this. Don't know how I missed it haha. Funny that we used the exact same argument as well otherwise I mightn't have noticed! – briantist – 2017-03-21T18:40:50.750

You beat me to it! – root – 2017-07-09T02:55:50.387

3

TI-Basic-83, 4 bytes

fPart(.5Ans

Gets the partial fraction part of 1/2 multipled by Ans, returns 0 if even, anything else if odd.

Magic Octopus Urn

Posted 2017-03-21T10:30:34.823

Reputation: 19 422

It's 11 bytes m8! – Daniel W. – 2017-07-10T07:50:04.743

1

@DanFromGermany Ti-Basic is a tokenized language.

– pizzapants184 – 2017-07-11T03:05:35.147

3

PHP, 12 bytes

echo$argn&1;

Run like this:

echo 123 | php -R 'echo$argn&1;';echo
  • Odd: 1
  • Even: 0

aross

Posted 2017-03-21T10:30:34.823

Reputation: 1 583

3

Rail, 29 bytes

$'main'
oi
r e
|  >{main}
\2-

Try it online!

Explanation

Every time I try to golf something in Rail, I remember how incredibly tricky it is, because of how strict the track placement rules are (especially for junctions). I think this is the first time, that I actually used recursion (or any method calls at all).

Rail programs start from the $, going southeast. i reads a character from STDIN. e checks for EOF. > is a three-way junction. It pops the result from the EOF check. If we haven't reached EOF yet, the train takes the left-turn. {main} is a recusive call to the main routine, which really just means that we'll start over from the beginning.

Once we've reached EOF, the train will take the right-turn at the junction. The -, \ and | on the remainder of the track are necessary to make the train take a couple of turns. 2 pushes 2, r computes the last digit modulo 2 (r for "remainder") and o outputs it. Technically, we'd have to terminate the track with #, but I ran out of space and the program exits either way, sadly, crashing the train.

This version of the trolley problem (do I save the trolley or do I save the bytes) seems to be more easily resolved than the classical one...

alternatively, also 29 bytes

$'main'
 |/i-
 |   >2ro
 \-e/

Try it online!

This one uses a physical loop in the tracks. The basic idea is the same: the track initially leads to the EOF-check e, after which we reach the 3-way junciton. As long as there is input left to reach, we read one character with i and loop back into the previous track. Once we reach EOF we compute and print the parity with 2ro.

Obviously, we still can't afford the extra byte to prevent the train from derailing.

Martin Ender

Posted 2017-03-21T10:30:34.823

Reputation: 184 808

3

K (oK), 2 bytes

2!

Try it online!

Dennis

Posted 2017-03-21T10:30:34.823

Reputation: 196 637

3

Lua, 20 bytes

print(io.read()%2<1)

This returns true if STDIN mod 2 is 0 and returns false if it isn't. You can also remove the <0 to get the opposite results.

Josh

Posted 2017-03-21T10:30:34.823

Reputation: 1 021

2You can change ==0 to <1. – Pavel – 2017-03-26T19:41:39.163

3

OIL, 39 bytes

5
0
9
0
10
0
1
16
9
9
0
10
0
1
17
2
4
4

Explanation:

5 0 reads the input into line 0. 9 0 decrements line 0 (using the fact that the input must be positive (i.e. >0)).

10 0 1 compares line 0 to line 1 (which contains a 0). If they're identical, that means the number must have been odd, jump to cell 16.

9 0 decrements line 0 again. 10 0 1 does the same comparison again, except this time equality means the number was even, jump to cell 17. Otherwise go on with the decrementation process (jump to line 2).

Line 16 and 17 both contain a 4 (print), meaning if we jumped to line 16, 4 4 will print what's in line 4 (10). If we jumped to line 17 instead, 4 (0) will print 0, the value in line 0.

L3viathan

Posted 2017-03-21T10:30:34.823

Reputation: 3 151

3

Neim, 1 byte

Try it online!

Explanation:

ᛄ: modulo 2 (implicit input and output)

user2059810

Posted 2017-03-21T10:30:34.823

Reputation: 31

3

Symbolic Python, 6 bytes

_&=_/_

Try it online!

Input and output happens through the variable _ - its initial value is the input, and its final value is the output.

The code works by returning the least significant bit, performing _ &= 1. However, as 1 is obviously not allowed in Symbolic Python, _/_ is used, which is of course 1 for all inputs.

FlipTack

Posted 2017-03-21T10:30:34.823

Reputation: 13 242

1_/_ since it's guaranteed to be positive – ASCII-only – 2019-03-13T06:49:06.260

@JoKing neither does the final result? – ASCII-only – 2019-03-27T03:58:49.440

3

Turing Machine Code, 99 bytes

0 * * r 0
0 _ _ l 1
1 0 0 * A
1 2 2 * A
1 4 4 * A
1 6 6 * A
1 8 8 * A
A * * * halt-e
1 * * * halt-o

Outputs via the halt state, terminating in state halt-e for even or halt-o for odd.

Try it online.

SuperJedi224

Posted 2017-03-21T10:30:34.823

Reputation: 11 342

wonder if outputting via the item under the cursor would be valid – ASCII-only – 2019-04-12T07:15:22.763

2

jq, 5 characters

.%2<1

Sample run:

bash-4.3$ jq '.%2<1' <<< 16384
true

On-line test:

manatwork

Posted 2017-03-21T10:30:34.823

Reputation: 17 865

Aww, Too bad that TIO doesn't have jq. – Matthew Roh – 2017-03-21T11:00:55.950

2

Ruby, 10 bytes

->x{x&1<1}

True for even, false for odd.

G B

Posted 2017-03-21T10:30:34.823

Reputation: 11 099

1Since both 0 and 1 are truthy, returning 0 or 1 wouldn't be allowed for ruby, right? – Eric Duminil – 2017-03-22T21:50:44.557

Also, are methods allowed for golf submission, or should they all look like functions : would odd? or even? be a correct submission? – Eric Duminil – 2017-03-22T21:53:55.280

1@EricDuminil Correct; in ruby 0 and 1 are both truthy. – Christopher Lates – 2017-03-23T20:11:01.763

2

Haskell, 8 bytes

(`mod`2)

1 for odd and 0 for even Test cases:

>(`mod`2)1
1
>(`mod`2)2
0
>(`mod`2)999999
1

Alternative with True/False output, 13 bytes

(==0).(`mod`2)

Test cases with repl usage:

>((<1).(`mod`2))1
False
>((<1).(`mod`2))2
True
>((<1).(`mod`2))16438
True
>((<1).(`mod`2))999999
False

Antisthenes

Posted 2017-03-21T10:30:34.823

Reputation: 181

3Um odd is a Prelude Haskell function. – Ørjan Johansen – 2017-03-21T15:39:04.800

2

Python 2 REPL, 9 bytes

input()%2

Sample run:

>>>input()%2
42
0

Outputs 0 for even and 1 for odd

Try it online!

Felipe Nardi Batista

Posted 2017-03-21T10:30:34.823

Reputation: 2 345

2

PHP, 14 bytes

<?=$argv[1]%2;

Try it online!

Outputs 1 for odd, 0 for even.

Business Cat

Posted 2017-03-21T10:30:34.823

Reputation: 8 927

2

GNU sed, 7 bytes

This is also a regex only solution. Input in unary is allowed for sed, based on this meta consensus. No truthy / falsy values actually exist in sed, as there are no data types.

Input as unary: 7 bytes. Output is 0 for odd numbers, and nothing for even ones.

s:00::g     # input consists of only zeros (4 -> 0000). Two zeros are deleted as
            #many times as possible. Remaining pattern space is printed implicitly.

Try it online!

Input as decimal: 12 bytes. Output is 1 for odd numbers, and something other than 1 for even.

/[13579]$/c1     # if last char is 1, 3, 5, 7 or 9, then change pattern space to 1.
                 # Otherwise, do nothing. Implicit printing done at the end.

Try it online!

seshoumara

Posted 2017-03-21T10:30:34.823

Reputation: 2 878

You could use d instead of c1 for the decimal version. sed doesn't really have anything truthy/falsy, and the empty string matches /^$/ so you could use that as your definition of truthy. – Riley – 2017-03-21T14:51:53.320

Or you could remove the second line all together and switch c0 to c1. Then odd outputs 1 and even outputs something other than 1. – Riley – 2017-03-21T14:54:05.037

@Riley I like your second idea. Initially, I used the least known sed command, =, to print 1 (as in number of input lines), but one needs the -n flag also to suppress implicit printing. – seshoumara – 2017-03-21T15:02:15.913

2

Groovy, 7 bytes

{!it%2}

Answer too short for posting.

Magic Octopus Urn

Posted 2017-03-21T10:30:34.823

Reputation: 19 422

2

SQLite, 10 bytes

SELECT x%2

Try it online!

Magic Octopus Urn

Posted 2017-03-21T10:30:34.823

Reputation: 19 422

2

Jelly, 2 bytes

Here's an alternate approach to this Jelly answer by ais523

BṪ

B   convert the input to binary
 Ṫ  and return its last character: 0 for even, 1 for odd.

Try it online!

Also at 2 bytes, the classical Modulo 2:

%2

Try it online!

which also returns 0/1 for even/odd resp.

steenbergh

Posted 2017-03-21T10:30:34.823

Reputation: 7 772

2

Forth, 11 bytes

: f 2 mod ;

Try it online

Output is 0 if even, 1 if odd.

mbomb007

Posted 2017-03-21T10:30:34.823

Reputation: 21 944

2

ZX Spectrum BASIC, 35 31 characters

Thanks to @Leo for removing 4 characters!

I don't know how to obtain the true (tokenized) program size, so the indicated score is in characters

1 INPUT a
2 PRINT a/2-INT(a/2)

Output is 0.5 (which is truthy) for odd input, and 0 (which is falsy) for even input.

This was tested on the Windows SpecBAS interpreter.

enter image description here

Luis Mendo

Posted 2017-03-21T10:30:34.823

Reputation: 87 464

1Is *2 necessary? – Leo – 2017-03-21T21:28:47.567

1@Leo It isn't! Thanks! – Luis Mendo – 2017-03-21T22:34:32.787

2

TacO, 29 bytes

   2i i
  i -+*2
@+%#?v
    1

Outputs 1\n if even, or just \n if odd.

This takes all multiples of 2 from 2 to 2n, removes all the ones in which n - i ~= 0 and sets the rest to 1, then sums that list.

Try it online!

ATaco

Posted 2017-03-21T10:30:34.823

Reputation: 7 898

2

Scratch, 4 blocks + 1 byte

ask [] and wait; say ((answer) mod (2));

Python equivalent:

i = input()
print(i % 2)

Returns 0 for even and 1 for odd.

OldBunny2800

Posted 2017-03-21T10:30:34.823

Reputation: 1 379

2

Perl 6, 4 bytes

*%%2

Try it

This is a WhateverCode lambda closure that takes one positional argument.

Expanded:

*   # the parameter   (turns the expression into a WhateverCode)
%%  # is divisible by
2   # two

Brad Gilbert b2gills

Posted 2017-03-21T10:30:34.823

Reputation: 12 713

2

Wise, 4 bytes

:><^

Try it Online! (Takes input through command line arguments for now)

Prints 1 for odd, 0 for even.

        Implicit input
:       Duplicate
 ><     Bitshift right, then left, which changes the least significant bit to 0
   ^    Xors with original
        Implicit output

A number is odd if the least significant bit is 1. By xoring the input and the input with the least significant bit as 0, we get 1 on odd numbers (0 != 1), and 0 on even (0 == 0)

MildlyMilquetoast

Posted 2017-03-21T10:30:34.823

Reputation: 2 907

2

BitCycle, 22 bytes

This program takes input on the command-line in unary; it outputs 1 for odd, nothing for even. You can also specify the -u flag and give input in decimal, in which case the output is 1 for odd, 0 for even.

?v
v<  <
A\\B^
>/\
  !

Explanation

Here's a slightly ungolfed version in action (the same logic with extra space added for clarity):

The program in action. Input: 5; output: 1 (for "odd")

The input bits come in at the source ? and are routed into the A collector. Once the whole input is in there, bits are emitted one at a time to the right. The two splitters \\ send the first two bits downward, while the remainder go into the B collector. (Splitters deactivate after the first time a bit hits them, so the second bit passes through the deactivated first splitter and is reflected by the second one.)

The second bit goes down, is reflected rightward by \, and goes off the playfield. Meanwhile, the first bit is reflected leftward by / and immediately sent rightward again by >. It passes through the two deactivated splitters and goes off the playfield. Finally, if there are any bits still in the B collector, they are now cycled around back to A. When the collectors come open, the splitters return to their original state, and the loop continues until there are less than two bits left.

If the number was even, there will be no bits left, in which case the program terminates without output. If the number was odd, there is a single bit left in A. The splitters \ and / send it down and left to the >, which sends it to the right. It goes back through the deactivated / and hits the bottom-right \. The latter has not been deactivated this time because there wasn't a second bit. So it reflects the single bit downward into the sink !, which outputs it.

DLosc

Posted 2017-03-21T10:30:34.823

Reputation: 21 213

2

Campfire, 8 bytes

Prints 1 for odd input or 0 for even input, then exits with an error.

&2&2%.%.

Campfire is an esoteric programming language I've created a couple of months ago. While it has a builtin for computing remainders, its unique flow of instructions makes even this simple challenge not so trivial (even if in this case the source turns out to be quite symmetric and nice).

In Campfire, after executing each instruction, the instruction pointer jumps to just after the next occurrence of the instruction just executed (wrapping between the end and the start of the code). Moreover, if the value on the top of the main stack is not 0, the instruction pointer is turned around before jumping (when backwards, it jumps to just before the previous occurrence of the executed instruction).

Here's an explanation of the execution of this program. * marks the instruction executed at each step, and <,>, or ? marks the spot where the instruction pointer will jump to, along with its direction.

&2&2%.%.
* <          & takes a number from input and pushes it to the stack.
             Since we're dealing only with strictly positive numbers,
             we will always switch direction.
 * >         Push 2 to the stack (then switch direction)
    * ?      Compute remainder. If the result is 1 we will switch direction,
             if it's 0 we won't.
 #First alternative:
&2&2%.%.
     * <     Output the result, the stack is now empty, and an empty stack
             has impicit 0s on it, so we won't switch direction.
      *      Try to compute remainder, but the stack is composed only by
             implicit 0s, so this will exit the program with an error.
 #Second alternative:
&2&2%.%.
     > *     We execute the other output instruction, result is the same.
      *      Again, we end by computing 0%0.

Leo

Posted 2017-03-21T10:30:34.823

Reputation: 8 482

2

TI-Basic, 4 bytes

fPart(Ans/2

Other solutions:

remainder(Ans,2         5 bytes - returns 0 even 1 odd

Returns 0 for even numbers and 1/2 for odd numbers. These values evaluate to false and true respectively in TI-Basic.

Timtech

Posted 2017-03-21T10:30:34.823

Reputation: 12 038

2

Taxi, 797 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:w 1 l 1 r.Pickup a passenger going to Divide and Conquer.Go to Starchild Numerology:e 1 l 1 l 1 l 2 l.2 is waiting at Starchild Numerology.Pickup a passenger going to Divide and Conquer.Go to Divide and Conquer:w 1 r 3 r 1 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 Equal's Corner.Pickup a passenger going to Trunkers.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 "O"if no one is waiting.1 is waiting at Writer's Depot.Switch to plan "E".[O]0 is waiting at Writer's Depot.[E]Go to Writer's Depot:n 1 l 1 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 r 2 r 1 l.

Try it online!

Ungolfed:

Go to Post Office: west 1 left, 1 right, 1 left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: west 1 left, 1 right.
Pickup a passenger going to Divide and Conquer.
Go to Starchild Numerology: east 1 left, 1 left, 1 left, 2 left.
2 is waiting at Starchild Numerology.
Pickup a passenger going to Divide and Conquer.
Go to Divide and Conquer: west 1 right, 3 right, 1 right, 2 right, 1 right.
Pickup a passenger going to Cyclone.
Go to Cyclone: east 1 left, 1 left, 2 left.
Pickup a passenger going to Equal's Corner.
Pickup a passenger going to Trunkers.
Go to Trunkers: south 1 left.
Pickup a passenger going to Equal's Corner.
Go to Equal's Corner: west 1 left.
Switch to plan "Odd" if no one is waiting.
1 is waiting at Writer's Depot.
Switch to plan "Even".
[Odd]
0 is waiting at Writer's Depot.
[Even]
Go to Writer's Depot: north 1 left 1 right.
Pickup a passenger going to Post Office.
Go to Post Office: north 1 right 2 right 1 left.

Erik the Outgolfer

Posted 2017-03-21T10:30:34.823

Reputation: 38 134

Note: If this turns out to be a dupe of Engineer Toast's answer, I'll delete it, even though consensus says dupes are allowed. – Erik the Outgolfer – 2017-03-23T15:29:55.643

This seems much shorter, and Taxi answers are generally similar so thats natural. – Matthew Roh – 2017-03-23T15:33:13.397

@SIGSEGV Wow the Taxi website is back online. – Erik the Outgolfer – 2017-03-23T15:49:38.077

2

Scala, 14 Bytes

(x:Int)=>x%2<1

Stefan Aleksić

Posted 2017-03-21T10:30:34.823

Reputation: 119

2

Vim Macro 54 Bytes

^vehxiF^[ve"nyo1234567890^MFTFTFTFTFT^[k@njvyo^[pi^[k$vggx

This Vim Macro works on any file that contains just an integer. During the cleanup process, it removes all the code above itself. This can probably be better implemented where it cleans up only the code it wrote but that would require more bytes.

The ^[ is escape (return to normal mode) and the ^M is the enter key.

What this basically does is it removes everything but the last digit of the number, prepends F to it, saves that into register n so now register n holds F<your number>. Then, it creates two new lines. One containing 1234567890 and the other line containing FTFTFTFTFT. Once it creates the second line, the cursor moves up, executes the n macro, thus finding backwards the first occurrence of <your number>, going down one line to the second line, yanking the corresponding true or false character from it, creating a new line, pasting that character, and then deleting everything it just did.

Vim Macros present a really interesting challenge and require you to think in a different way than what you're used to. It was super cool to work on this.

Stefan Aleksić

Posted 2017-03-21T10:30:34.823

Reputation: 119

2

Funciton, 68 bytes

Byte count assumes UTF-16 encoding with BOM.

╓─╖
║p║
╙┬╜┌┐
 ├─┤├
╔╧╗└┘
║1║
╚═╝

Try it online!

This defines a function p which takes a single integer and returns 0 for even and 1 for odd numbers.

Explanation

╓─╖
║p║
╙┬╜

This is the function declaration header. The line leaving the box will emit the input of the function when called.

╔╧╗
║1║
╚═╝

This is a literal. The line leaving the box will emit a 1.

 ├─

This T-junction computes the bitwise NAND of its inputs, and emits it to the right.

   ┌┐
   ┤├
   └┘

Finally, we compute the bitwise NOT, by splitting the value on first T-junction and computing the bitwise NAND with itself on the second T-junction.

The loose end on the right is used as the function's output value. Hence, the overall function computes:

p(x) = NOT(NAND(x, 1)) = AND(x, 1)

Martin Ender

Posted 2017-03-21T10:30:34.823

Reputation: 184 808

2

PHP, 14 bytes

<?=$argv[1]&1;

When I test for odd or even, I only look at the first bit and not % the whole integer.

Daniel W.

Posted 2017-03-21T10:30:34.823

Reputation: 121

2

Klein, 18 17 16 + 3 bytes, (non-competing)

Uses the 001 topology. Embedded in a Klein Bottle

1(/+@
-+)-($?/:1

Explanation

Here is a gif of it running on 6:

enter image description here

The general way this works is it continually decrements the input down to zero each time multiplying a saved value by -1. That way the saved value will be negative if odd and positive if even.

Setup

Right as the program begins running it does its set up

1(

This puts a 1 on the scope. This is the number we will multiply by -1 to keep track of the parity.

We then use a mirror to deflect the ip of the top of the screen. Since we are on a Klein Bottle and not a Torus this deflection will not only bring us to the bottom but also flip our horizontal coordinate. The ip will move upwards through mostly blank space until it hits another mirror. This mirror puts it in the main loop.

Main Loop

In the main loop we make a duplicate of the input and decrement it by one, multiply the scoped value by -1 using )-( and then we switch the copied version of the counter with the original, we copied it earlier because ? our conditional jump consumes a value. Since ? skips the mirror we will run until our counter is zero.

However since we are checking the copy of the counter, we will actually exit when we hit -1. This is important because it saves us from having to make a value later.

Cleanup

Once we hit zero we are deflected again but the mirror. This sends us over the top of the screen. Since we are on a Klein Bottle this twists us back around and causes the ip to collide with the ) used in part of the code. This is convenient because it recalls the scoped value to the stack. We then get deflected by one of the mirrors from before. Lastly we add the two values, the counter and the saved value. The counter will always be -1 so we are essentially just subtracting 1 from the saved value. The saved value will be either 1 or -1 so our result is either -2 or 0. Then we end and output using @.

Post Rock Garf Hunter

Posted 2017-03-21T10:30:34.823

Reputation: 55 382

2

newline 19 bytes

g\n0\n/\n[\nd\n]

and the number 2

Christopher

Posted 2017-03-21T10:30:34.823

Reputation: 3 428

2

Dodos, 13 bytes

	main dip dip

Try it online!

This program is composed by a single main function which decrements twice its number and then recurses. Due to how Dodos works, instead of infinitely recursing the first call that would go to a negative number will return its argument instead, which will be either 0 or 1.

Leo

Posted 2017-03-21T10:30:34.823

Reputation: 8 482

You can make a P function.

– Erik the Outgolfer – 2018-06-01T20:20:22.097

2

INTERCAL, 45 bytes

DOWRITEIN:1PLEASE:1<-:1~#1DOREADOUT:1DOGIVEUP

Try it online!

Prints 1 if the input is odd and 0 if the input is even.

...except this being the wonderful Compiler Language With No Pronounceable Acronym, 1 is printed as newline-I-newline while 0 is printed as underscore-newline-newline, and input has to have the individual digits spelled out in one of several natural languages or a mix thereof (such that the third test case, 16384, is just as well English ONE SIX THREE EIGHT FOUR as Basque-Latin-Georgian-Tagalog-Kwakiutl BAT SEX SAMI WALO MU). Also, I'm not actually sure which INTERCAL dialect this is.

DO WRITE IN :1      Take a line of input and store the value in the variable :1.
PLEASE :1<-:1~#1    Politely reassign :1 to be the first bit of its old value.
DO READ OUT :1      Print :1 in "butchered Roman numerals".
DO GIVE UP          End the program.

Unrelated String

Posted 2017-03-21T10:30:34.823

Reputation: 5 300

2

Keg, 2 3 bytes

^2%

Last number outputted will be 1 if the number is odd, and 0 if it's even

Edit: Fixed for latest version

Try it Online!

EdgyNerd

Posted 2017-03-21T10:30:34.823

Reputation: 1 106

This seems to be for an old version, before 2019-08-13. Unfortunately the unconditional implicit input used by this code does not exist in current version.

– manatwork – 2019-08-20T15:52:20.717

Fixed, don't know why that got removed – EdgyNerd – 2019-08-20T15:54:34.953

Uhm… You probably mean ^2%, thinking to multidigit numbers. – manatwork – 2019-08-20T15:55:55.900

Fixed again, oops – EdgyNerd – 2019-08-20T15:58:00.537

2% works again now. – Lyxal – 2019-11-15T21:33:11.957

2

Cascade, 7 5 bytes

#2&
%

Found a 2 bytes shorter solution by abusing wrapping Try it online!

EdgyNerd

Posted 2017-03-21T10:30:34.823

Reputation: 1 106

2

Toi, 6 bytes

(r[rr]

Try it online!

Toi is a language that only knows sets. The memory is a set S that starts as the empty set. The input number can be given as a von Neumann ordinal, constructed using "ua"*n, or alternatively the input number can be represented by the nesting level using "u"*n. u nests the context S, and a changes S to \$S \cup \{t \mid t \in s, s \in S\}\$ (from the esolangs wiki article).

( cond [ stuff ] is a while loop that performs stuff while applying cond on S yields a non-empty set. The commands in cond are unapplied before executing into stuff.

r changes S to \$\{t \mid t \in s, s \in S\}\$ essentially removes a layer of nesting. This effectively decrements the number represented by S by 1.

So, the meaning of ( r [ rr ] is the following: while r on S is non-empty (i.e. while S-1 is not 0), perform rr (decrement S twice).

The parity is left on S, \$\{\{\}\}\$ if the input is odd, and \$\{\}\$ otherwise.

user41805

Posted 2017-03-21T10:30:34.823

Reputation: 16 320

2

RPL, 4 bytes

2MOD

Plain answer in vanilla RPL, for posterity and completeness of this thread.

As often with RPL, one byte is saved in the source code by removing a space between a number and a command. I assume this is rarely observed in Forth.

Nacre

Posted 2017-03-21T10:30:34.823

Reputation: 81

2

Shakespeare Programming Language, 137 bytes

Try it online!

#.Ajax,.Puck,.Act I:.Scene I:.[Enter Ajax and Puck]Ajax:Listen tothy.You is the remainder of the quotient betweenyou a big cat.Open heart

Fairly simple, just outputs the input mod 2. Maybe improvable.

Hello Goodbye

Posted 2017-03-21T10:30:34.823

Reputation: 442

Oof, why didn't I test and why is this language so weird. Rolled back. Thank you! I just tested to make sure it didn't error lol. – Hello Goodbye – 2019-12-15T01:52:33.760

2

Poetic, 190 bytes

although i agree i,a person,i was dismissive about a hell,i am not anarchic
i repeat:i know theres never a hell i am doomed to
actually,it seems a bit of a fraud,a lie,if He gives a man he|l

Try it online!

Input is in decimal. Output is an Unexpected EOF error for even inputs, and a Mismatched IF/EIF error for odd inputs. It takes forever to actually execute, though.

(Doesn't work for 0, but the question only specified positive inputs.)

JosiahRyanW

Posted 2017-03-21T10:30:34.823

Reputation: 2 600

2

Rust, 148 bytes

use std::io;fn main(){let mut d=String::new();io::stdin().read_line(&mut 
 Rd);d=d.trim().to_string();let b=d.parse::<i32>().unwrap()%2;print!("{}",b)}

I'm really bad with rust, so this probably isn't very optimal, but I didn't see any other rust answers.

Try it online!

Matthew Wozniak

Posted 2017-03-21T10:30:34.823

Reputation: 21

3Hey, welcome to the Code Golf Stack Exchange! I hope you enjoy your stay here. Nice answer by the way! – Lyxal – 2019-12-16T04:37:32.297

Is there any point to the b variable, or the added d assignment? Why not just define a function that takes a number rather than all the reading from stdin stuff? – Jo King – 2019-12-16T06:27:04.583

If you want to keep this as a full program, you can use "".into() instead of String::new() to save a few bytes. – Esolanging Fruit – 2019-12-18T22:56:05.883

1

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)

steenbergh

Posted 2017-03-21T10:30:34.823

Reputation: 7 772

1

Brachylog, 3 bytes

%₂0

Try it online!

Explanation

Well…

%₂0     Input mod 2 = 0

Fatalize

Posted 2017-03-21T10:30:34.823

Reputation: 32 976

1

Cardinal, 7 bytes

%++~:M.

Outputs truthy if odd falsy if even

Try it online!

fəˈnɛtɪk

Posted 2017-03-21T10:30:34.823

Reputation: 4 166

1

AHK, 21 Bytes

a=%1%
Send % Mod(a,2)

Once again, I can't use inputs directly in functions so I have to store it as another variable.

Engineer Toast

Posted 2017-03-21T10:30:34.823

Reputation: 5 769

1

C, 15 bytes

#define f(n)n&1

Returns 0 if even, 1 if odd.

Steadybox

Posted 2017-03-21T10:30:34.823

Reputation: 15 798

1

Common Lisp, 7 Bytes

#'evenp

Built-in function that checks parity. Barely beating Java!

MatthewRock

Posted 2017-03-21T10:30:34.823

Reputation: 913

1

Julia 0.5, 5 bytes

isodd

Try it online!

Dennis

Posted 2017-03-21T10:30:34.823

Reputation: 196 637

1

><>, 4 + 3 = 7 bytes

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

2%n;

Try it online!

PidgeyUsedGust

Posted 2017-03-21T10:30:34.823

Reputation: 631

1

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%

seshoumara

Posted 2017-03-21T10:30:34.823

Reputation: 2 878

1

V, 8 7 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 ò

user41805

Posted 2017-03-21T10:30:34.823

Reputation: 16 320

Cool approach, I like it! Alternate (but very similar) approach: Àé1Ó11 – James – 2017-03-21T20:34:42.697

1

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

MildlyMilquetoast

Posted 2017-03-21T10:30:34.823

Reputation: 2 907

1

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
              -   [c+1]     | subtract (adds)

Conor O'Brien

Posted 2017-03-21T10:30:34.823

Reputation: 36 228

1

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.

MildlyMilquetoast

Posted 2017-03-21T10:30:34.823

Reputation: 2 907

1

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

Bijan

Posted 2017-03-21T10:30:34.823

Reputation: 781

Alternatively, 2|]. – Conor O'Brien – 2017-03-21T18:37:58.843

um... what does the ']' do? – Bijan – 2017-03-21T18:51:51.137

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 – 2017-03-21T19:24:35.217

An APL expression for all the 3-byte J expressions that are valid answers – Adám – 2017-03-23T10:59:23.580

1

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.

seshoumara

Posted 2017-03-21T10:30:34.823

Reputation: 2 878

1

Fortran 95,  31  28 bytes

Thanks to @georgewatson for saving three bytes!

function h(i)
h=mod(i,2)
end

Returns 0 if even, 1 if odd.

Try it online!

Steadybox

Posted 2017-03-21T10:30:34.823

Reputation: 15 798

Since input is always a positive integer, you could save 3 bytes by using mod. – georgewatson – 2018-02-01T10:18:53.423

@georgewatson Thanks! – Steadybox – 2018-02-01T14:19:41.710

1

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!

seshoumara

Posted 2017-03-21T10:30:34.823

Reputation: 2 878

You can golf it by removing the newlines. – Okx – 2017-03-23T14:27:06.797

@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 – 2017-03-23T15:13:38.377

1

tinylisp, 18 bytes

There's a library function for that.

(load library)odd?

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.

DLosc

Posted 2017-03-21T10:30:34.823

Reputation: 21 213

1

LibreLogo, 19 bytes

Code:

print int input "%2

Result:

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

Grant Miller

Posted 2017-03-21T10:30:34.823

Reputation: 706

1

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.

steenbergh

Posted 2017-03-21T10:30:34.823

Reputation: 7 772

1

Jellyfish, 6 bytes

p|i
 2

Try it online!

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

Martin Ender

Posted 2017-03-21T10:30:34.823

Reputation: 184 808

1

Clojure, 15 bytes

#(= 0(rem % 2))

Ignoring the obvious built-ins, this is probably as short as it gets.

Checks if the argument divided by 2 equals 0. Nothing fancy. Returns true for even numbers, false for odd.

Beaten by brainfuck!

Carcigenicate

Posted 2017-03-21T10:30:34.823

Reputation: 3 295

1

Dogescript, 32 29 bytes

such f much n
return n%2
wow

Returns 1 if n is odd, 0 otherwise.

vpzomtrrfrt

Posted 2017-03-21T10:30:34.823

Reputation: 201

You could still keep the Dogescript syntax and save 3 bytes: such f much n return n%2 wow. There's no need to convert a truthy value to boolean. – Grant Miller – 2017-03-31T20:44:13.107

1

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

user41805

Posted 2017-03-21T10:30:34.823

Reputation: 16 320

That's okay. Every answer else is trivial too. At least it's not a builtin. – Matthew Roh – 2017-03-23T09:32:57.373

Why do you have to convert to float? – Hello Goodbye – 2019-11-14T16:27:36.067

@HelloGoodbye Because it is by default a string, although yes, I should work just as well – user41805 – 2019-11-14T16:57:56.877

ah, makes sense – Hello Goodbye – 2019-11-14T17:05:52.993

1

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.

G B

Posted 2017-03-21T10:30:34.823

Reputation: 11 099

Huh. Looks okay. – Matthew Roh – 2017-03-23T09:49:35.560

1

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!

Adám

Posted 2017-03-21T10:30:34.823

Reputation: 37 779

1

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.

ETHproductions

Posted 2017-03-21T10:30:34.823

Reputation: 47 880

High level language, low level answer (lol) – Matthew Roh – 2017-03-23T13:37:16.880

1

braingasm, 3 bytes

Takes input from stdin, prints 0 for even and 1 for odd.

;o:

; reads an integer, o checks the "oddity" (opposite of parity), : prints an integer.

edit: changed to p to o to retrofit to a breaking change in the language.

daniero

Posted 2017-03-21T10:30:34.823

Reputation: 17 193

2Congratulations on being the 100th answer to this question :P – math junkie – 2017-03-23T14:19:45.767

1

Qwerty-RPN, 4 bytes

@2%#

Explanation:

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

Okx

Posted 2017-03-21T10:30:34.823

Reputation: 15 025

1

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!

Okx

Posted 2017-03-21T10:30:34.823

Reputation: 15 025

1

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

Dead Possum

Posted 2017-03-21T10:30:34.823

Reputation: 3 256

1

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

Cyoce

Posted 2017-03-21T10:30:34.823

Reputation: 2 690

1

Elisp, 12 10 bytes

(%(read)2)

(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!

Christopher Pitts

Posted 2017-03-21T10:30:34.823

Reputation: 91

1

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

Plasmatic

Posted 2017-03-21T10:30:34.823

Reputation: 11

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 – 2017-03-25T12:40:14.980

1Welcome to the site! – caird coinheringaahing – 2017-03-31T06:40:13.000

1

Python REPL, 3 bytes

_&1

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

Aashutosh Rathi

Posted 2017-03-21T10:30:34.823

Reputation: 121

Just so you know, the downvote was cast automatically by the Community user when you edited your answer. I consider this a bug.

– Dennis – 2017-03-26T15:08:48.357

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 – 2017-03-26T16:08:59.553

1Welcome to PPCG! – Pavel – 2017-03-26T19:40:12.847

@ГригорийПерельман thank you – Aashutosh Rathi – 2017-03-28T07:33:55.510

@Dennis thanks for edit – Aashutosh Rathi – 2017-03-28T07:34:13.090

1

J, 2 bytes

2|         NB. remainder of arg / 2

    So 0 = true (even), 1 = false (odd)
    2| 7   => 1
    2| 10  => 0 

Richard Donovan

Posted 2017-03-21T10:30:34.823

Reputation: 87

This doesn't look right. I get a syntax error. I think you need another byte: 2|] or 2&|

– Adám – 2017-05-08T08:55:55.160

1

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

DLosc

Posted 2017-03-21T10:30:34.823

Reputation: 21 213

1

Scala, 9 bytes

(_:Int)%2

This outputs 1 for odd, 0 for even

sprague44

Posted 2017-03-21T10:30:34.823

Reputation: 81

1

x86_64 Linux machine language, 5 bytes

0:       97                      xchg   %edi,%eax
1:       83 e0 01                and    $0x1,%eax
4:       c3                      retq

To try it, compile and run the following C program

#include<stdio.h>
const char f[]="\x97\x83\xe0\1\xc3";
int main(){
  for( int i = 0; i < 10; i++ ) {
    printf("%d %d\n", i, ((int(*)(int))f)(i) );
  }
}

Try it online!

ceilingcat

Posted 2017-03-21T10:30:34.823

Reputation: 5 503

1

Aceto, 6 bytes, non-competing

(Non-competing because the question predates the language.)

%2
rip

ri reads a string and casts it to an integer, 2% does modulo 2, and p prints. 0 stands for even and 1 for odd in the printed result.

Laura Bostan

Posted 2017-03-21T10:30:34.823

Reputation: 181

1This is very likely the theoretical minimum. – L3viathan – 2017-05-08T08:31:30.360

2rip Aceto, postdated the challenge – Matthew Roh – 2017-05-08T08:32:32.867

1:) @L3viathan is indeed the theoretical minimum. – Laura Bostan – 2017-05-08T08:33:40.207

1

REXX 13 BYTES

say arg(1)//2

Try it here

REXX functions and instructions

theblitz

Posted 2017-03-21T10:30:34.823

Reputation: 1 201

1

Actually, 2 bytes

1&

Try it online!

Output is 0 if even, 1 if odd. The code is very straightforward: bitwise-AND (&) with 1 (return low bit of input).

Mego

Posted 2017-03-21T10:30:34.823

Reputation: 32 998

1

WC, 40 bytes

WIP mini language I created.

;>_0|;>(?#@8|!@3|//##@2|!@2|//#$-$-/)*$

Explanation:

;>_0|          New var using the first artifact (the input number)
;>(            Start a new function
  ?            Select the first variable (reset var index)
  #@8|         Start if statement with 9th global ("-1") as condition
    !@3|       Print the 4th global ("1")
    //         Terminate program
  #            End if statement
  #@2|         Start if statement with 3rd global ("0") as condition
    !@2|       Print the 3rd global ("0")
    //         Terminate program
  #            End if statement
  $-$-         Decrement the current variable twice
  /            Restart context (the current function)
)              End function
*$             Call the current variable (the function)

Try it online!

Jaggler3

Posted 2017-03-21T10:30:34.823

Reputation: 21

Cool looking language. Welcome to the site! :) – James – 2017-06-16T20:06:39.873

1

,,,, 1 byte

œ

Yeah, yeah, a builtin... For anybody wondering about the choice of character, it's supposed to be odd or even or o or e.

However, I do have other solutions.

2 bytes

1&

Explanation

1&

    implicit input
1   push 1
 &  pop input and 1 and push input & 1 (bitwise AND)
    implicit output

2 bytes

2%

Explanation

2%

    implicit input
2   push 2
 %  pop input and 2 and push input % 2 (modulo)
    implicit output

totallyhuman

Posted 2017-03-21T10:30:34.823

Reputation: 15 378

œ yeah a new solution, and a new builtin – Matthew Roh – 2017-07-12T03:28:25.387

1

Aceto, 5 bytes

ri2%p
r reads input
i converts to integer
2% takes the number modulo 2
p prints the answer

Try it online!

FantaC

Posted 2017-03-21T10:30:34.823

Reputation: 1 425

1

Underload, 5 bytes

():aS

Underload doesn't really have a concept of Truthy/Falsey, so this is empty/nonempty output for odd/even respectively.

Input should be in unary (as some number of ~) between the a and the S.

Explanation

() pushes the empty string. : duplicates it and () wraps the top stack element in parenthesis, meaning the stack now looks like "", "()". The input now appears in the program, meaning it is executed as code. ~ is the swap instruction in Underload, meaning that an even input is an even number of swaps, which doesn't affect the stack, and an odd input is an odd number of swaps, which has the same effect as a single swap. S outputs the top stack element, which will be either () if the input was even, and if the output was odd.

I mentioned that Underload doesn't have Truthy/Falsey, but the common way to represent booleans as Underload code that either swaps or doesn't swap, meaning that the cheaty one-byte program S technically works as a valid submission.

Esolanging Fruit

Posted 2017-03-21T10:30:34.823

Reputation: 13 542

1

Pyt, 2 bytes

2%

Explanation

            Implicit input
2%          Mod 2
            Implicit print

Odd is truthy, even is falsy

Try it online!


Alternatively, also 2 bytes

2|

Explanation:

            Implicit input
2|          Is it divisible by 2?
            Implicit output

Even is truthy, odd is falsy

Try it online!

mudkip201

Posted 2017-03-21T10:30:34.823

Reputation: 833

1

Z80Golf, 9 8 bytes

00000000: d5d2 0380 e601 ff76                      .......v

Try it online!

Disassembly

start:
  push de
  jp nc, $8003
  and 1
  rst $38
  halt

Golfed a byte using the "input loop at start" pattern:

  push de      ; Push 00 00 (return address) to the stack
  jp nc, $8003 ; Escape the loop if carry is set (EOF)
               ; otherwise take next input and return to the start of the program

Previous solution, 9 bytes

00000000: cd03 8030 fbe6 01ff 76                   ...0....v

Try it online!

Disassembly

start:
  call $8003
  jr nc, start
  and 1
  rst $38
  halt

Accepts the decimal input (binary, octal, hexadecimal, or any other even base would work), and outputs ASCII 0 for even, 1 for odd.

call $8003 calls getchar, which stores the next char to register a, or sets the carry flag on EOF. Since the only significant data is the last char, the program simply calls getchar repeatedly until EOF. and 1 takes the parity, rst $38 is a golf idiom for call putchar, and halt terminates the program.

9 bytes, Human-readable output

00000000: cd03 8030 fbe6 31ff 76                   ...0....v

Try it online!

Limiting the input to decimal or lower even bases, we can get more readable result ('0' = $30 and '1' = $31) by changing and 1 into and $31. Also works for hexadecimal if you use 0123456789pqrstu, case sensitive :)

Bubbler

Posted 2017-03-21T10:30:34.823

Reputation: 16 616

1

Python 2, 13 bytes

lambda n:~n&1

Try it online!

No shorter than the other non-REPL Python answers, but uses slightly different logic. Takes the bitwise complement of the input number (-x-1) and performs a BITWISE AND on it. Returns 1 if the complement is odd, and 0 if the complement is even - and therefore returns 1/0 if the original number was even/odd.

(Could be shortened by 1 byte by removing the complement ~ if 0=True and 1=False is allowable.)

Triggernometry

Posted 2017-03-21T10:30:34.823

Reputation: 765

1

Flobnar, 6 bytes

&
%@
2

Try it online! (requires the -d flag)

I don't think it's going to get much shorter than this.

Esolanging Fruit

Posted 2017-03-21T10:30:34.823

Reputation: 13 542

1

CHIP-8 assembly, 22 bytes

0x6001 F10A 410F 120C 8210 1202 8226 8F03 FF29 D005 1214

Takes in a number entered by the user, until it is terminated by the user pressing the F key, and prints out to the screen 0 if it's odd, or 1 if it's even.

The CHIP-8 had 16 8-bit registries (V0 to VF), with VF mostly being used for carry operations. The ROM was loaded into memory at address 0x200, which is why the jump operations are offset. It also contained display-representations of the numbers 0-9 and letters A-F in memory at 0x00, so that a programmer need not create them.

The code works like this:

0x200:      60 01        Set V0 to 0x01.
0x202:      F1 0A        Wait for a key to be pressed, and enters the value into V1.
0x204:      41 0F        Skip the next instruction if V1 does not equal 0x0F.
0x206:      12 0C        Jump to instruction address 0x20C.
0x208:      82 10        Assign the value of V1 into V2.
0x20A:      12 02        Jump to instruction address 0x202.
0x20C:      82 26        Store the least significant bit of V2 into VF, left-shift V2 by one bit, and store the shifted value into V2.
0x20E:      8F 03        Set VF to VF XOR V0 (V0 is set previously to 1, so this is equivalent to NOT VF).
0x210:      FF 29        Set the memory address pointer (I) to the character representation of the value in VF.
0x212:      D0 05        Draw the data from the address pointer I to the screen, at pixel (0, 0), with a width of 5 pixels (the height is always 8 pixels).
0x214:      12 14        Jump to instruction address 0x214 (i.e., loop indefinitely).

Ciaran_McCarthy

Posted 2017-03-21T10:30:34.823

Reputation: 689

1

Little Man Computer, 31 bytes, 5 instructions

INP
SUB 5
BRP 1
ADD 5
OUT
DAT 2

Which boils down to this once assembled into RAM:

901 205 801 105 902 002

Outputs \$ 1 \$ for an odd number and \$ 0 \$ for an even number.

A online emulator can be found here, though you may need to increase the speed to avoid waiting a painfully long time (if anyone finds a better online version, please enlighten me).

FlipTack

Posted 2017-03-21T10:30:34.823

Reputation: 13 242

1

Alchemist, 79 35 bytes

-43 bytes thanks to ASCII-only!!

-1 byte thanks to Jo King!

_->s+In_i
e+i->
0e+i->e
s+0i->Out_e

Outputs 0 for even and 1 for odd, try it online!

Ungolfed

ბიმო

Posted 2017-03-21T10:30:34.823

Reputation: 15 345

You could probably golf the outputs to... 0 and 1 or "" and "1" or something – ASCII-only – 2019-01-29T08:05:37.983

56? – ASCII-only – 2019-01-29T08:07:33.397

1

if other outputs (0/1 for odd/even) are allowed, 36

– ASCII-only – 2019-01-29T08:10:07.770

@ASCII-only That doesn't work since the output can occur before the input. 35 bytes should work deterministically

– Jo King – 2019-03-13T06:43:05.227

1

Regex (ECMAScript), 7 bytes

^(xx)*$

Try it online!

The input is in unary, as the length of a string of xs. It matches even numbers and returns "no match" for odd numbers.

This works in all regex flavors, even formal regular expressions, which lack some ECMAScript regex features (backreferences and lookaheads).

There is no shorter (6 bytes or less) unary ECMAScript regex function (with nonnegative integer input and match/no-match output) that requires the (, ), *, or + symbols.

Deadcode

Posted 2017-03-21T10:30:34.823

Reputation: 3 022

1

Come Here, 68 bytes

ASKi0CALLi*256iCOME FROM SGN(i//256)1CALL(i//256)iTELL48+(iMOD2)NEXT

Less golfed:

ASK i
0 CALL i*256 i
COME FROM SGN(i//256)
1 CALL (i//256) i
TELL 48 + (i MOD 2) NEXT

COME HERE uses integers internally, but handles input and output as strings; unfortunately, the string encoding it uses really isn't all that helpful for working with input as numbers. In this case, we have to repeatedly divide by 256 until we get a value that is less than 256, in order to get the last character.

If little-endian (rather than big-endian) input is allowed, we can use this shorter version (22 bytes) instead:

ASKiTELL48+(iMOD2)NEXT

Prints 0 for even, and 1 for odd.

SuperJedi224

Posted 2017-03-21T10:30:34.823

Reputation: 11 342

1

ZX80 (4K ROM) 23 tokenised BASIC bytes

 1 INPUT A
 2 PRINT A;":"; NOT A AND 1
 3 CONTINUE

This is a very simple solution as in Sinclair ZX80 BASIC, PRINT 111 AND 1 will produce 1 (and therefore any odd number entered will produce 1 because in binary for any odd positive integer the zeroth bit is always set, so PRINT 11 AND 1 will do 00001011 & 00000001 at the processor level more or less) - if we assume even is true then we can use NOT to invert the value, therefor NOT 111 AND 1 will produce 0 or our false value.

Using the CONTINUE command will continue the symbolic listing from line 1. You may also use RUN but both are equal in the basic tokens used.

ZX80 (4K ROM) 16 tokenised BASIC bytes

If we assume that odd is our true value then we can remove some of the logic as follows (also saving bytes):

 1 INPUT A
 2 PRINT A;":";A AND 1
 3 RUN

There is one caveat; by using ZX80 BASIC, you may only enter 16-bit wide signed integers, so the range is -32768 to +32767 inclusive.

The same logic will work in Commodore BASIC (C64/VIC-20) so whilst both of the above will work, you may condense it into one line like 0inputa:printa":"aand1:goto.

ZX80 4K ROM edition

Shaun Bebbers

Posted 2017-03-21T10:30:34.823

Reputation: 1 814

1

Pyramid Scheme, 221 bytes

    ^        ^ ^
   / \      /l\-
  /set\    /oop\
 ^-----^  ^-----^
 -    ^- /*\   / \
     ^- ^---^ /set\
    /#\ -^  -^-----^
   ^--- /-\  -    /-\
  /l\  ^---^     ^---^
 /ine\/1\  -     -  /2\
 --------           ---

Try it online!

Outputs 0 for even and 1 for odd.

Pyramid Scheme has no modulus, no integer division, and no bitwise operators. The best approach I could conceive was repeatedly subtracting two from the input until either it or its predecessor is zero, then printing the final value.

This program translates rather easily to true Scheme. I have to say, that's my favorite part of this language—it's very nearly a Scheme dialect in its own right, despite being a joke!

(define n (read))
(do ()
    ((= 0 (* n (- 1 n))))
  (set! n (- n 2)))
(display n)

Try it online!

Khuldraeseth na'Barya

Posted 2017-03-21T10:30:34.823

Reputation: 2 608

1

Re:direction, 7 bytes

♦♦
▼►
◄

Outputs 1 if odd, and 0 if even. Basically works by alternating between the left and right side until a is found ( is at the end of the input) and then outputs depending on which side it is on.

Try it online!

EdgyNerd

Posted 2017-03-21T10:30:34.823

Reputation: 1 106

1

Ruby, 6 bytes

n.odd?

Try it online!

I don't know if this is cheating. There's a method definition in the header. Is there a way to skip doing this and pass in a number differently, perhaps?

odd? is a default operation in Ruby.

AJFaraday

Posted 2017-03-21T10:30:34.823

Reputation: 10 466

Unless there is a way to define anonymous functions in Ruby, I believe this is a snippet, not a complete solution, unfortunately – caird coinheringaahing – 2019-10-05T17:49:05.460

1

Commodore BASIC (C64/128, VIC-20, TheC64/Mini, C16/+4) - 21 tokenized bytes

In this one, odd numbers produce a TRUE value and even numbers produce a FALSE (though on Commodore BASIC, FALSE is zero and TRUE is negative one; it does not do this, more conforms to modern-days TRUE/FALSE integer equivalents)

 0 inputa:ifatH?a":"aaN1

Expanded and non-minimised as:

 0 input a
 1 if a then print a; ":"; a and 1

Commodore VIC-20 rulez! Donkeysoft MMXIX

Shaun Bebbers

Posted 2017-03-21T10:30:34.823

Reputation: 1 814

1

MarioLANG, 37 bytes

;>-)+([!)
="=====#:
)![(-)-<
:#====="

Try it online!

Outputs 1 for odd and 0 for even.

CoedRhyfelwr

Posted 2017-03-21T10:30:34.823

Reputation: 272

1

Kotlin, 15 bytes

{n:Int->n%2<1}

smac89

Posted 2017-03-21T10:30:34.823

Reputation: 121

1

W, 2 bytes

2m

Basically just modulo by 2...

user85052

Posted 2017-03-21T10:30:34.823

Reputation:

0

PARI/GP, 6 bytes

n->n%2

Mod 2.

alephalpha

Posted 2017-03-21T10:30:34.823

Reputation: 23 988

0

CJam, 4 bytes

q~2%

Try it online!

Simple mod by 2. Outputs 1 for odd, 0 for even.

Business Cat

Posted 2017-03-21T10:30:34.823

Reputation: 8 927

0

Java, 91 bytes

enum d{;public static void main(String[]a){System.out.println(Long.parseLong(a[0])%2==0);}}

Takes number as first command line argument and outputs to STDOUT.

cookie

Posted 2017-03-21T10:30:34.823

Reputation: 271

0

bc, 9 bytes

Output is 1 for odd, and 0 for even.

read()%2

Try it online!

A trailing newline is necessary to run successful, and it is included in the bytes count. The read() function works only for numerical input.

seshoumara

Posted 2017-03-21T10:30:34.823

Reputation: 2 878

0

Applescript function, 26

on f(i)
return i mod 2
end

Test calls

log f(1)
log f(2)
log f(3)

Digital Trauma

Posted 2017-03-21T10:30:34.823

Reputation: 64 644

0

Go, 26 bytes

func(i int)int{return^i&1}

Returns 1 for even and 0 for odd.

Try it online!

powelles

Posted 2017-03-21T10:30:34.823

Reputation: 1 277

0

Scala, 15 14 5 3 bytes

_%2

Returns 0 if it's an even number and 1 if it's odd

This is equivalent to (i:Int)=>i%2.
However, this needs to be assigned to a variable of type Int=>Int, otherwise the compiler will complain.
The whole code would look like this:

val x: Int => Int = _%2

2xsaiko

Posted 2017-03-21T10:30:34.823

Reputation: 699

Just a tip but you can reduce this by one byte by doing <1 instead of ==0. – Stefan Aleksić – 2017-03-25T14:46:59.927

I don't think that's cheating. See here. Notice that Java 8 lambda solutions often leave off the type.

– Brian McCutchon – 2017-03-26T23:44:33.520

Is the <1 even needed? would it not return 0 if even and 1 if odd? – fəˈnɛtɪk – 2017-03-27T14:49:44.727

Depends if we need a boolean, but I guess returning 0 and 1 works too. Thanks! – 2xsaiko – 2017-03-27T19:37:19.400

0

√ å ı ¥ ® Ï Ø ¿ , 4 bytes

Ik@o

Try it online!

Ignore the header/footer on TIO; that's the interpreter for √ å ı ¥ ® Ï Ø ¿

Not as trivial as the other answers that use builtins or %. Outputs 0 for odd and 1 for even.

I    - Take input
 k   - Remove all odd numbers from the stack
  @  - Push the length of the stack
   o - Output

If the input is odd, it is removed from the stack and the length pushed is 0.

If it's even however the length will be 1 (the input)

caird coinheringaahing

Posted 2017-03-21T10:30:34.823

Reputation: 13 702

0

Swift, 18 bytes

let f={n in n%2<1}

John McDowall

Posted 2017-03-21T10:30:34.823

Reputation: 161

Can be shortened to let f={n in n%2} which would return 0 for even and 1 for odd, saving 2 bytes – Mr. Xcoder – 2017-04-11T14:09:31.417

0

Python 2, 16 bytes

print(input()%2)

Try it online!

Amol

Posted 2017-03-21T10:30:34.823

Reputation: 21

3Oh I thought you were using Python Two-Thirds – Matthew Roh – 2017-04-01T00:15:03.470

This code will spit me an error message. – Dat – 2017-04-16T23:46:48.723

1this code is python 2 only, in python 3 it would be: print(int(input())%2) – Felipe Nardi Batista – 2017-05-04T11:09:53.787

Save a byte with print input()%2 – Josh – 2017-05-19T21:37:15.870

0

Chip, 2 bytes

For the simple case:

Aa

This only handles one-byte numbers, either a single ASCII digit or a 1 byte unsigned integer (because ASCII code points for odd digits are themselves odd). This works like the &1 bitmask used in many other answers. If multiple bytes are provided, this will treat each new byte as a separate input. Outputs 0x0 for even and 0x1 for odd.

By adding e*f, we can get output in ASCII (5 bytes):

Aae*f

Try it online!

23 + 3 = 26 bytes

For the complex case:

 Svvvvvvvv~t
azABCDEFGH

This handles numbers of any length, ASCII or unsigned integer as before (big endian so that least significant bit is still last). Requires either a null terminator, or the -z flag (as shown in the byte count) to detect the end on the input. Like above, outputs 0x0 for even and 0x1 for odd.

Again, we can add e*f to get ASCII output (25 + 3 = 28 bytes):

*fSvvvvvvvv~t
eazABCDEFGH

Try it online!

Phlarx

Posted 2017-03-21T10:30:34.823

Reputation: 1 366

0

JavaScript, 6 bytes

A function of input n that returns 0 if even and 1 if odd.

n=>n%2

Blue Okiris

Posted 2017-03-21T10:30:34.823

Reputation: 157

0

x86 machine code, 2 bytes

00000000  a8 01                                             |..|
00000002

Sets the zero flag if the value in register A is even.

user5434231

Posted 2017-03-21T10:30:34.823

Reputation: 1 576

0

R, 10 bytes

!scan()%%2

If you can get away with assuming n is the value, !n%%2 works.

user11599

Posted 2017-03-21T10:30:34.823

Reputation: 191

0

Convex, 3 bytes

Ã2%

Try it online!

GamrCorps

Posted 2017-03-21T10:30:34.823

Reputation: 7 058

0

Python 3, 21 bytes

print(int(input())%2)

Try it online!

Dat

Posted 2017-03-21T10:30:34.823

Reputation: 879

Hello, and welcome to PPCG! This is 17 bytes. However, you need some way of outputting it (i.e. print). – NoOneIsHere – 2017-04-16T23:21:20.280

@NoOneIsHere, may I know how did you calculate the size? – Dat – 2017-04-16T23:22:10.493

You can calculate the size for example with this online tool. In addition, if there aren't any special Unicode characters in the code, the size in bytes is usually the number of characters.

– Steadybox – 2017-04-16T23:41:24.043

You can use Python 2 for a few less bytes: print input()%2 is 15 bytes. – NoOneIsHere – 2017-04-17T00:10:24.513

0

///, 5 bytes

/00//

Try it online!

Takes input in unary. Input goes after the last /. Prints a 0 of it's even, otherwise prints nothing. Incredibly short for a /// program that actually does soething.

Comrade SparklePony

Posted 2017-03-21T10:30:34.823

Reputation: 5 784

0

x86 machine code, 3 bytes

83 E0 01 (and ax, 1 in nasm)

Explanation:

Anding any number with 1 will return 1 if the number is odd, or 0 if it is even.

 1111 (15)
&0001
-----
 0001

 1110 (14)
&0001
-----
 0000

 0001 (1)
&0001
-----
 0001

 0010 (2)
&0001
-----
 0000

So move a number into AX, use the three bytes, and AX will hold 0 or 1, if the number is even or odd.

user233009

Posted 2017-03-21T10:30:34.823

Reputation: 389

0

J-uby, 5 Bytes

~:%&2

Takes the reversed-arguments version of % and partially applies 2 to it, creating a function which takes x and returns x%2. 0 for even, 1 for odd.

Cyoce

Posted 2017-03-21T10:30:34.823

Reputation: 2 690

0

Casio-Basic, 9 bytes

mod(n,2)

8 bytes for the function, 1 to add n as an argument.

numbermaniac

Posted 2017-03-21T10:30:34.823

Reputation: 639

0

Triangular, 6 bytes

$.2%m<

Prints 0 for even, 1 for odd. Try it online!

  $
 . 2
% m <

$ reads an integer to the stack. 2 pushes 2 to the stack. m divides the top two stack values and pushes the remainder. % prints as an integer.

MD XF

Posted 2017-03-21T10:30:34.823

Reputation: 11 605

0

Common Lisp, 4 bytes

oddp

Builtin function, like Haskell odd.

Renzo

Posted 2017-03-21T10:30:34.823

Reputation: 2 260

0

Check, 4 bytes (non-competing)

>2%p

Explanation: >2 pushes 2 onto the stack, % preforms modulus with the implicit input, and then p prints the result.

Esolanging Fruit

Posted 2017-03-21T10:30:34.823

Reputation: 13 542

What is "Check"? – Cyoce – 2017-06-17T07:49:43.570

@Cyoce https://github.com/ScratchMan544/check-lang.

– Esolanging Fruit – 2017-06-17T21:55:23.297

0

Implicit, 2 bytes

_2

Prints 0 (falsy) for even, 1 (truthy) for odd. Try it online!

     « implicit integer input  »;
_2   « mod top of stack by 2   »;
     « implicit output         »;

MD XF

Posted 2017-03-21T10:30:34.823

Reputation: 11 605

0

WolframAlpha, 6 bytes (non-competing)

x mod2

Try it online!

This will output 0 if the value is divisible by 2 and a non-zero value ("truthy") otherwise.

However, if that is not good enough, there's an alternative 11-byte solution using the floor function:

⌊x⌋mod2

Try it online!

Note: Byte count in UTF-8 counted using this resource.


Obviously, Wolfram is not a programming language. However, at the time of writing, the question was ambiguous enough to not explicitly disallow this answer. In spite that, I have marked this as non-competing. Cheers!

Matheus Avellar

Posted 2017-03-21T10:30:34.823

Reputation: 273

0

expr, 6 bytes

$1 % 2

Note expr(1) is a POSIX standard utility and is not a shell built in. It first arrived in UNIX V7.

Normal mod 2. Alternatively could have used a bitwise &

gdahlm

Posted 2017-03-21T10:30:34.823

Reputation: 119

0

ES2018, 8 Bytes

x=o=>o%2

Or, in ES5+typed arrays+intl:

ES5, 25 Bytes

function x(o){return o%2}

user74225

Posted 2017-03-21T10:30:34.823

Reputation: 11

0

SNOBOL4 (CSNOBOL4), 30 27 bytes

	OUTPUT =REMDR(INPUT,2)
END

Try it online!

Prints 1 for odd, 0 for even.

SNOBOL doesn't have true or false and all conditions are done on SUCCESS or FAILURE so this is sort of truthy?

Giuseppe

Posted 2017-03-21T10:30:34.823

Reputation: 21 077

0

Brain-Flak, 10 bytes

({{}[{}]})

Try it online!

Takes input via unary. Odd corresponds to 1, even to 0. Basically alternates between adding and subtracting the unary values to a total until it runs out of numbers and pushes the total. If the number is odd, then there is an extra 1 and it returns 1.

Jo King

Posted 2017-03-21T10:30:34.823

Reputation: 38 234

0

17, 40 bytes

1 for odd, 0 for even. It takes input until a newline is entered and then prints mod 2 of last byte. It is not competing because 17 was made after the question was released, through not specifically made for this challenge.

0{I : a == 0 @}1{0 @ 2 % $$}777{0 0 @}

Hugh Williams

Posted 2017-03-21T10:30:34.823

Reputation: 51

Welcome to the site! We allow languages to be created after the challenge so no need to bother with "not competing". – Post Rock Garf Hunter – 2018-02-04T20:48:31.837

0

Momema, 13 bytes

z*-8 01z0-8*0

Try it online! Outputs 0 for odd, 1 for even.

Explanation

Momema's control flow is in the form of named jumps, which also function as labels. Ungolfed, this program is:

z   *-8
0   1
z   0
-8  *0

Each jump instruction takes an argument n and evaluates it. Its semantics are "jump past n jump instructions from here, ignoring any that don't share my label and cyclically wrapping around the program as necessary if one is not found."

That last part—the cyclic wrapping—is important here. An even parameter will continue searching for jump instructions and finish on the first jump instruction, and an odd parameter will finish on the second jump instruction. In the first case, the assignment statement will fill cell 0 (initially with value 0) with value 1. After that, it encounters a jump instruction, but since the argument is 0, it has no effect. Finally, in both cases, -8 outputs the value of cell 0 in decimal, and its value will be set to 0 if and only if the input integer was even.

If outputting nothing for odd/something for even is allowed, the following program works for 10 bytes:

z*-8-8 1z0

Esolanging Fruit

Posted 2017-03-21T10:30:34.823

Reputation: 13 542

0

Piet, 10 codels

Codel size 40

Test using this online interpreter, with codel size 40.

Does what you might expect: take input, push 2, apply the mod operation, and output the result. The program then terminates due to the J-shape at the right-hand edge.

Odd (respectively, even) input corresponds to output of 1 (respectively, 0).

Note: the lower-left-hand codel could be changed to black, white, or almost any other color without changing the program's behavior. However, if that codel is changed to "dark green" to match the codel to its right, then the program will calculate the remainder modulo 3 instead of 2.

So this program can trivially be modified to become a mod-3 calculator, with the same number of codels.

Bonus mod-3 calculator (not an answer to this challenge):

enter image description here

mathmandan

Posted 2017-03-21T10:30:34.823

Reputation: 943

0

Whitespace, 30 bytes

[S S S T    S N
_Push_2][S N
S _Duplicate][S N
S _Duplicate][T N
T   T   _Read_STDIN_as_number][T    T   T   _Retrieve][S N
T   _Swap_top_two][T    S T T   _Modulo][T  N
S T _Output]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Outputs 0 for even, 1 for odd.

Try it online.

Explanation (with 5 as input):

Command    Explanation             Stack      Heap     STDIN    STDOUT

SSSTSN     Push 2                  [2]        {}
SNS        Duplicate top (2)       [2,2]      {}
SNS        Duplicate top (2)       [2,2,2]    {}
TNTT       Read STDIN as number    [2,2]      {2:5}    5
TTT        Retrieve                [2,5]      {2:5}
SNT        Swap top two            [5,2]      {2:5}
TSTT       Modulo                  [1]        {2:5}
TNST       Output top as number    []         {2:5}             1

Kevin Cruijssen

Posted 2017-03-21T10:30:34.823

Reputation: 67 575

0

SmileBASIC, 13 bytes

INPUT N?N<<31

SB uses 32 bit integers, so shifting left by 31 places will preserve only the lowest bit. Even numbers become 0 (falsey), odds become -2147483548 (truthy).

Works on both positive and negative numbers.

12Me21

Posted 2017-03-21T10:30:34.823

Reputation: 6 110

0

ActionScript 2.0, 36 bytes

function a(b){trace(!Boolean(b%2));}
function a(b){                       - define a function taking one argument:
              trace(                 - output
                    !                - the opposite of
                     Boolean(        - the next bit converted to boolean:
                             b%2     - the function's argument mod 2
                                ));} - finish defining the function

a(2); traces 'true', a(3); traces 'false'

Note: it could probably be made smaller if it didn't matter what the output format was, but I think this is pretty small if it has to be a boolean, and you could obviously cut it down by one if you didn't mind true meaning odd and false meaning even.

Jhynjhiruu Rekrap

Posted 2017-03-21T10:30:34.823

Reputation: 61

0

Perl 5, 6 bytes

Add +1 for -p

#!/usr/bin/perl -p
$_%=2

Try it online!

Ton Hospel

Posted 2017-03-21T10:30:34.823

Reputation: 14 114

0

Tcl, 19 bytes

puts [expr $argv%2]

Try it online!

sergiol

Posted 2017-03-21T10:30:34.823

Reputation: 3 055

0

Charm, 11 bytes

[ 2 / pop ]

Try it online!

LyricLy

Posted 2017-03-21T10:30:34.823

Reputation: 3 313

0

C (gcc), 109 99 bytes

#include <stdio.h>
void main(){int n;scanf("%d",&n);if(n&1)printf("%d:1",n);else printf("%d:0",n);}

Try it online!

C (gcc), 82 bytes : If we are only returning Integers 0 or 1 and not explicitly printing.

#include <stdio.h>
int main(){int n;scanf("%d",&n);if(n&1)return 0;else return 1;}

Try it online!

EDIT: Thanks @user202729 for your suggestions! They've been implemented and it cut a good 10 bytes off the code! Wasn't able to implement the printf() and scanf() functions without the #include statement so going to look some more into that. 0 now means True (or Odd in this case) and 1 now means False (or Even in this case).

A brief explanation:

Originally, I was going to use the printf() prototype instead of including stdio.h. However, the need for scanf() to take input ruined the C minimalism a bit. :)

First we initialize an integer "n", our number we're checking parity for. Then we scanf() for the user to enter the value of n. Since we are able to determine if a number is odd or even by determining if it's first bit (farthest to the right, lowest) is set or unset (1 or 0). In this case, a bit set to 1 means the number is odd, whereas a bit set to 0 means the number is even. The code checks for Odd (True), and else Even.

Marhtini

Posted 2017-03-21T10:30:34.823

Reputation: 11

Welcome to PPCG! (1) IF this is GCC you don't need #include. Just use it. – user202729 – 2018-06-01T03:02:22.883

(2) use conditional operator should be able to save a lot of bytes. (3) By "truthy/falsy value" it don't need to be exactly the string True and False, just 0 and 1 are enough. (see Interpretation of Truthy/Falsy)

– user202729 – 2018-06-01T03:04:21.070

(4) YOu can remove two pairs of {}s. – user202729 – 2018-06-01T03:04:56.490

Thanks for the guidance and the welcome @user202729! First time with code golfing, trying to be more efficient with my code so I figured Golf Challenges were the way to go. – Marhtini – 2018-06-01T03:18:02.943

Save 70 bytes Approximately – Muhammad Salman – 2018-06-01T13:03:15.277

Nice @MuhammadSalman, trying to figure out the rules a bit for CodeGolf. Are the #include statement, method declaration, and print functions required as part of the solution byte count? – Marhtini – 2018-06-01T14:19:46.010

@Marhtini : I am very bad at C but here is a general version of what you can and can't under tips for golfing for C

– Muhammad Salman – 2018-06-01T14:26:30.880

0

C (gcc), 17

This does not require any headers. & is bitwise AND; it runs way faster than regular operators. Example: f(7)=> 7 is 111 => 111 AND 1 is 1. If a number's last digit in base 2 is 0, it is even. Else, it is odd. And &1 runs way faster than %2.

f(i){return i&1;}

Windmill Cookies

Posted 2017-03-21T10:30:34.823

Reputation: 601

You could and should provide a link so it can be tested. – Muhammad Salman – 2018-06-01T13:17:31.427

0

Python REPL/Shell, 9 bytes

input()&1

returns 0 for even and 1 for odd

Khalil

Posted 2017-03-21T10:30:34.823

Reputation: 49

3This answer doesn't actually seem to output anything. I'd suggest either making it a lambda or using print to make it actually produce output. – Post Rock Garf Hunter – 2018-06-01T17:43:08.457

4We require submissions to be functions (whether named or unnamed), or complete programs, which means you would need a print statement. – mbomb007 – 2018-06-06T22:04:04.517

1Note that this answer becomes valid if you change the language to Python REPL. – Dennis – 2018-06-07T04:39:24.107

0

HadesLang, 20 bytes

raw:[in:[]] % 2 is 0

in:[] Reads from console
raw:[] Converts a string to any required datatype

Azeros

Posted 2017-03-21T10:30:34.823

Reputation: 41

0

Eukleides, 20 bytes

print number()mod 2

It's trivial, but at least Eukleides has an entry now. number() is the function to take a number as input, and you're supposed to pass it a string to use as a prompt. I thought for sure I'd had to actually pass an empty string before, but I'm not getting an error, so I'll take it. Since it's a simple mod 2, 1 means odd and 0 means even.

brhfl

Posted 2017-03-21T10:30:34.823

Reputation: 1 291

0

JavaScript (Node.js), 6 bytes

Prints 0 for even, 1 for odd

a=>a&1

Try it online!

Luis felipe De jesus Munoz

Posted 2017-03-21T10:30:34.823

Reputation: 9 639

0

Unnamed, 15 Bytes

This takes an input and prints 1 if its odd, 0 if even.

; # _%.@0.2 # .

How?

" # " is the command separator.

;   take input as integer and put it into the cuurent cell (0)
_   assign                                    to the current cell (0)
%.                                   modulo 
@0.2       the value in the 0th cell        2
.   print the value in the current cell

Try it (paste the code into the first textarea and write your input to the second one. press run and the output will appear in the third area)

Windmill Cookies

Posted 2017-03-21T10:30:34.823

Reputation: 601

0

Ahead, 5 bytes

I2%O@

1 == true == odd. 0 == false == even.

Try it online!

snail_

Posted 2017-03-21T10:30:34.823

Reputation: 1 982

0

Pepe, 14 bytes

rEeEreEEEEreEE

Try it online! (Fails in large cases like 99999999999999999)

0 for even, 1 for odd

Explanation:

rEeE           # input as int
    reEEEE     # ...mod 2
          reEE # output int

u_ndefined

Posted 2017-03-21T10:30:34.823

Reputation: 1 253

0

Tamsin, 19 bytes

main={"0"&"0"}&eof.

Try it online!

Takes input in unary. Recognizes an even number of 0 characters.

Esolanging Fruit

Posted 2017-03-21T10:30:34.823

Reputation: 13 542

0

Gol><>, 5 bytes

I2%zh

take input, push 2, modulo a, b, then if the number was even, output 1, otherwise output 0

Try it online!

KrystosTheOverlord

Posted 2017-03-21T10:30:34.823

Reputation: 681

0

BotEngine, 3x9=27 (28 bytes)

v  02468
>IRSSSSST
   >>>>>F

Outputs TRUE for odd and FALSE for even.

SuperJedi224

Posted 2017-03-21T10:30:34.823

Reputation: 11 342

0

Microscript, 4 bytes

2si%

Microscript II, 4 bytes

2sN%

Prints 1 for odd and 0 for even.

SuperJedi224

Posted 2017-03-21T10:30:34.823

Reputation: 11 342

0

Pyramid Scheme, 185 149 bytes

     ^
    /=\
   ^---^
  ^-  /#\
 ^-  ^---
/#\ /+\
---^---^
  /"\ ^-
 ^---/"\
 -^ ^---
 //\-
^---^
-^ /2\
/#\---
---^
  / \
 /arg\
^-----
-^
/1\
---

Try it online!

Uses a different method from Khuldraeseth's answer, and outputs 1 for odd numbers, 0 for even. This takes input as a command line argument. This basically equates to:

a = int(input())/2
print(a == int(str(a) + str(0)))

This works because a string like "0.5"+"0" is converted to the float 0.5, whereas "1"+"0" will be 10. This means numbers already with a decimal point will ignore the added 0.

Jo King

Posted 2017-03-21T10:30:34.823

Reputation: 38 234

0

Underload, 10 bytes

(0)(1)()^S

Input is in hard-coded in Unary, using ~ in the 3rd bracket. 0 for odd, 1 for even.

Try it Online!

EdgyNerd

Posted 2017-03-21T10:30:34.823

Reputation: 1 106

0

AsciiDots, 16 bytes

.-#?{&}$#
 .-#1/

Outputs 0 if even, and 1 if odd. Basically just a Boolean AND with 1.

Try it online!

EdgyNerd

Posted 2017-03-21T10:30:34.823

Reputation: 1 106

0

05AB1E, 1 byte

É

Try it online!

Explanation

   # implicit input
É  # push 1 if odd, otherwise push 0

Sagittarius

Posted 2017-03-21T10:30:34.823

Reputation: 169

0

MineFriff, 7 bytes

Ii2,%o;

Explanation:

Ii` {Change the input mode to (I)nteger and take input}
2,` {Push 2 onto the stack}
%o` {x % 2 and output}
;   {end prog}

Lyxal

Posted 2017-03-21T10:30:34.823

Reputation: 5 253

0

Python 3, 12 bytes

lambda n:n&1

Outputs 1 for odd, 0 for even.

Sagittarius

Posted 2017-03-21T10:30:34.823

Reputation: 169

-1

Javascript <= ES5, 28 bytes

function i(n){return n%2==1}

If returning 1 or 0 is allowed, than (27 bytes):

function i(n){return n%2&1}

user68281

Posted 2017-03-21T10:30:34.823

Reputation:

The second one is invalid JS, but the first is ok. – Rɪᴋᴇʀ – 2017-04-17T21:40:57.930

@Riker fixed... – None – 2017-04-17T21:43:14.257

1 or 0 is okay, yes. Also, you might be able to make this an anonymous function or lambda to save some bytes. – Rɪᴋᴇʀ – 2017-04-17T21:52:03.793

You don't need the &1 at all, since -1 and 1 are both "truthy" and returning -1, 1, and 0 is allowed since it passes the "truthiness" test, i.e., they're valid values to place in an if() statement and be evaluated as you would expect. – Patrick Roberts – 2017-05-17T04:43:59.043

-1

Scala 8 bytes

a=>a%2<1

Similar to the Java 8 solution and probably should also include type to compile like this:

 val m:Int=>Boolean=a=>a%2<1 

user88272

Posted 2017-03-21T10:30:34.823

Reputation: 1

Welcome to the site. If you include an explanation of your code (even if short) It will be long enough to fix the title formatting. – Post Rock Garf Hunter – 2019-07-30T13:06:09.520

Thank you.. I will do that – user88272 – 2019-07-30T14:38:42.770