74
\$\begingroup\$

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>

\$\endgroup\$
15
  • 2
    \$\begingroup\$ This isn't the first time I've confused mathematical with computational parity... this is a code site after all! \$\endgroup\$
    – Neil
    Mar 21 '17 at 10:38
  • \$\begingroup\$ Since this is pretty much one of these(1,2,3) questions, it should probably have a snippet to see all the answers. \$\endgroup\$ Mar 21 '17 at 13:57
  • 4
    \$\begingroup\$ @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. \$\endgroup\$
    – mbomb007
    Mar 21 '17 at 16:37
  • 2
    \$\begingroup\$ @tuskiomi The challenge only asks about positive integers. (0 is considered even but not positive) \$\endgroup\$ Mar 22 '17 at 2:46
  • 4
    \$\begingroup\$ @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! \$\endgroup\$ Mar 23 '17 at 15:09

239 Answers 239

1
2
3 4 5
8
5
\$\begingroup\$

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.
\$\endgroup\$
2
  • \$\begingroup\$ Would #?#%! or _?#%! work too? \$\endgroup\$ Mar 21 '17 at 17:09
  • \$\begingroup\$ @MistahFiggins yes. \$\endgroup\$ Mar 21 '17 at 17:10
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

Excel, 10 bytes

=MOD(A1,2)

Or:

=ISODD(A1)

For output of:

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

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I've never seen excel in code golf... \$\endgroup\$
    – user58826
    Mar 25 '17 at 13:10
  • 1
    \$\begingroup\$ Alternate 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 \$\endgroup\$ Mar 25 '17 at 19:55
5
\$\begingroup\$

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
\$\endgroup\$
5
\$\begingroup\$

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

\$\endgroup\$
8
  • \$\begingroup\$ I think you can output 0/1 instead of true/false. alert(prompt()%2) seems to be 9384 chars. \$\endgroup\$ Mar 21 '17 at 19:19
  • \$\begingroup\$ 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. \$\endgroup\$
    – Luke
    Mar 21 '17 at 19:27
  • \$\begingroup\$ @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. \$\endgroup\$
    – powelles
    Mar 21 '17 at 19:54
  • \$\begingroup\$ @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. \$\endgroup\$ Mar 21 '17 at 19:55
  • \$\begingroup\$ The code I pastied is like a function name. Just append the parentheses and include the argument in it. \$\endgroup\$
    – Luke
    Mar 21 '17 at 20:13
4
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Your program only echoes the MOD result, which is incorrect. The question said the output format should be"(Input):(Output)" \$\endgroup\$
    – stevefestl
    Mar 21 '17 at 11:24
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Save one byte by using bitwise operators: 1AND a is equivalent to a MOD 2. \$\endgroup\$
    – DLosc
    Mar 22 '17 at 4:42
4
\$\begingroup\$

Haskell, 3 bytes:

odd

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

\$\endgroup\$
4
\$\begingroup\$

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)
\$\endgroup\$
1
4
\$\begingroup\$

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

\$\endgroup\$
2
  • 1
    \$\begingroup\$ If I may: !scan()%%2. \$\endgroup\$
    – plannapus
    Mar 21 '17 at 14:26
  • 1
    \$\begingroup\$ Don'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. \$\endgroup\$ Mar 21 '17 at 16:11
4
\$\begingroup\$

Flurry -nii, 14 12 10 bytes

{}{{}[]}{}

Try it online!

Found a better zero-one alternating function: \x. x 0, and realized that the zero can be fetched from the stack height. Also, changing the last {} to [] gives 0 for even and 1 for odd, essentially implementing n%2.

(\x. x 0) 1 = 1 0 = 0^1 = 0
(\x. x 0) 0 = 0 0 = 0^0 = 1

Flurry -nii, 14 12 bytes

{}{{}(){}}{}

Try it online!

Easier one found by Esolanging Fruit. Basically applies the function "return 1 if the input is 0, return 0 if the input is 1" n times to 1.

-2 bytes because the inner I {{}} could be fetched from the empty stack as {}.

//     {} {...}  {}
main = n  swap01 1
//       {   {} () {} }
swap01 = \x. x  K  I
// swap01 0 = 0 K I = I = 1    (K applied zero times to I)
// swap01 1 = 1 K I = K I = 0  (K applied once to I)

Flurry -nii, 24 bytes

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

Try it online!

How it works

Uses the "succ chain" trick: the expression succ succ ... succ (succ repeated k times) with a Church numeral argument n gives a polynomial on n:

succ n = n + 1
succ succ n = n^2 + n
succ succ succ n = n^2 + n + 1
succ succ succ succ n = n^3 + n^2 + n
...

In general,

  • succ repeated 2k times gives n^(k+1) + n^k + ... + n
  • succ repeated 2k+1 times gives n^(k+1) + n^k + ... + n + 1

Alternatively, even-th repetition multiplies by n and odd-th repetition adds 1.

Such an alternating behavior can be used to distinguish between even and odd numbers. If you repeat succ n times to 0, it cycles between "add 1" and "times 0", effectively giving 0 and 1 for even and odd respectively. But we can't simply concatenate succ n times; we need to apply succ n times to I.

X = {{}[<><<>()>]}  \x. x succ
                    apply succ to given function
main = {}X{}[<>()]  n X I 0
                    apply X n times to I (giving length-n chain of succ)
                    then call it with 0

n = 1 -> main = succ 0 = 1
n = 2 -> main = succ succ 0 = 0
n = 3 -> main = succ succ succ 0 = 1
...
\$\endgroup\$
1
  • \$\begingroup\$ 14 byter: {}{{}(){{}}}{} (returns 1 for even and 0 for odd) \$\endgroup\$ Aug 21 '20 at 22:28
3
\$\begingroup\$

Perl 5, 6 bytes

5 bytes of code + -p flag.

$_%=2

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Clojure, 4 bytes

odd? 

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

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

Ohm, 2 bytes

è

Explanation:

è: (pop()%2)==1
Ohm also has implict input and output
\$\endgroup\$
3
\$\begingroup\$

PHP, 14 bytes

<?=$argv[1]%2;

Try it online!

Outputs 1 for odd, 0 for even.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ $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! \$\endgroup\$
    – briantist
    Mar 21 '17 at 18:40
  • \$\begingroup\$ You beat me to it! \$\endgroup\$
    – root
    Jul 9 '17 at 2:55
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

PHP, 12 bytes

echo$argn&1;

Run like this:

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

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.

\$\endgroup\$
3
\$\begingroup\$

K (oK), 2 bytes

2!

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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)
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can change ==0 to <1. \$\endgroup\$
    – Pavel
    Mar 26 '17 at 19:41
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ It's 11 bytes m8! \$\endgroup\$
    – Daniel W.
    Jul 10 '17 at 7:50
  • 1
    \$\begingroup\$ @DanFromGermany Ti-Basic is a tokenized language. \$\endgroup\$ Jul 11 '17 at 3:05
3
\$\begingroup\$

Neim, 1 byte

Try it online!

Explanation:

ᛄ: modulo 2 (implicit input and output)
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ wonder if outputting via the item under the cursor would be valid \$\endgroup\$
    – ASCII-only
    Apr 12 '19 at 7:15
1
2
3 4 5
8

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .