81
\$\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\$
13
  • 3
    \$\begingroup\$ This isn't the first time I've confused mathematical with computational parity... this is a code site after all! \$\endgroup\$
    – Neil
    Commented Mar 21, 2017 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\$ Commented Mar 21, 2017 at 13:57
  • 3
    \$\begingroup\$ @tuskiomi The challenge only asks about positive integers. (0 is considered even but not positive) \$\endgroup\$ Commented Mar 22, 2017 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\$ Commented Mar 23, 2017 at 15:09
  • 1
    \$\begingroup\$ As @HelkaHomba mentioned, zero is not positive; what is the expected result for input of zero? Or do we not care? \$\endgroup\$
    – Richard
    Commented Mar 26, 2017 at 18:42

264 Answers 264

1
2
3 4 5
9
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\$ Commented Mar 21, 2017 at 17:09
  • \$\begingroup\$ @MistahFiggins yes. \$\endgroup\$ Commented Mar 21, 2017 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\$
3
  • 1
    \$\begingroup\$ I've never seen excel in code golf... \$\endgroup\$
    – user58826
    Commented Mar 25, 2017 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\$ Commented Mar 25, 2017 at 19:55
  • \$\begingroup\$ Cool, but VBA is a different language from Excel formulas.. \$\endgroup\$
    – roblogic
    Commented Oct 6, 2022 at 11:14
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\$ Commented Mar 21, 2017 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
    Commented Mar 21, 2017 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
    Commented Mar 21, 2017 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\$ Commented Mar 21, 2017 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
    Commented Mar 21, 2017 at 20:13
5
\$\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\$
5
\$\begingroup\$

KonamiCode, 111 109 bytes

>>>>(^^)v(>)>(^)v(>)L(>)>(^)v((>))>(^^)S(^)A(^^)v(^)A(^>)L(^^)v(>)B(^^^)L(^^^)L(^>)>(>)S((^))>(^)B(>)>(^^)<<<

Explanation

KonamiCode lacks a modulo function, so I'm detecting if a number is odd or even using the following algorithm:

While a counter is less than the number to check:
    If x is 0:
        Set x to 1
    Else:
        Set x to 0
Output the number

This has the effect of having x be 0 if the input is even, and 1 if it is odd.

Annotated code: (I'm using x to refer to the x value in the above example)

>>> [Raw input, to get the number to check]
[The next few instructions set up the values in memory]
>(^^) [Sets up "x"]
v(>)
>(^) [Sets up the counter]
v(>)
L(>)
  [We are now inside a loop.]
  >(^)
  v((>)) [Increase the counter]
  >(^^) [Go to x's position in memory (address 2)]
  S(^) [Set the comparison buffer to 1]
  A(^^) [If the comparion buffer equals the current memory value, go to L(^^).
  This will happen if x is 1.]
  [This point is only reached if x is not 1]
  v(^) [Sets x to 1]
  A(^>) [If x is 1 jump to L(^>)

  L(^^) [If x was 1 we jumped to here]
  v(>) [Sets x to 0]
  B(^^^) [If x is not 1 jump to L(^^^)


  L(^^^)
  L(^>)
  [Finally, we reset everything and loop again]
  >(>)
  S((^))
  >(^)
B(>)
[Print the result and finish.]
>(^^)
<<<
\$\endgroup\$
5
\$\begingroup\$

Swift Compiler, chained types, 57  43 39 bytes

Way shorter than the other one but way less fun.

struct A{enum T{typealias T=A}}
_=A.T()

SwiftFiddle link

Input is the number of T's on the last line: 1 is A.T, 2 is A.T.T, and so on. The program only compiles if there is an even number of T's.

Swift Compiler, nested types/tag system, 565 bytes

protocol P{associatedtype T:P
associatedtype A:P
associatedtype B:P
associatedtype C:P}
protocol Q{}
enum H:P,Q{typealias T=H
typealias A=H
typealias B=H
typealias C=H}
enum E:P,Q{typealias T=H
typealias A=a<E>
typealias B=b<E>
typealias C=c<E>}
struct a<T:P>:P{typealias A=a<T.A>
typealias B=a<T.B>
typealias C=a<T.C>}
extension a:Q where T.T.C.B:Q{}
typealias Z<T:P>=a<T>
enum b<T:P>:P{typealias A=b<T.A>
typealias B=b<T.B>
typealias C=b<T.C>}
extension b:Q where T.T.A:Q{}
enum c<T:P>:P{typealias A=c<T.A>
typealias B=c<T.B>
typealias C=c<T.C>}
let x:Q=Z<a<E>>()

SwiftFiddle link

The input is the number of a's on the last line, so Z<a<E>> is 1, Z<a<a<E>>> is 2, and so on. The program only compiles if there is an even number of a's.

Prior work: this Swift forums post in which Slava Pestov embedded a tag system at the type level.

If the reverse condition is allowed -- truthy for odd and falsy for even -- Z can be removed, for 540 bytes.

\$\endgroup\$
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
    Commented Mar 21, 2017 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\$

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\$
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
    Commented Mar 22, 2017 at 4:42
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
    Commented Mar 21, 2017 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\$ Commented Mar 21, 2017 at 16:11
4
\$\begingroup\$

Neim, 1 byte

Try it online!

Explanation:

ᛄ: modulo 2 (implicit input and output)
\$\endgroup\$
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\$ Commented Aug 21, 2020 at 22:28
4
\$\begingroup\$

Vyxal, 1 byte

Try it Online!

0 for even, 1 for odd.

Alternatively,

Try it Online!

1 for even, 0 for odd.

\$\endgroup\$
1
  • \$\begingroup\$ okay this is unbelievable but again this is a great answer \$\endgroup\$ Commented Jul 14, 2021 at 1:45
3
\$\begingroup\$

Perl 5, 6 bytes

5 bytes of code + -p flag.

$_%=2

Try it online!

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

Ruby, 10 bytes

->x{x&1<1}

True for even, false for odd.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Since both 0 and 1 are truthy, returning 0 or 1 wouldn't be allowed for ruby, right? \$\endgroup\$ Commented Mar 22, 2017 at 21:50
  • \$\begingroup\$ Also, are methods allowed for golf submission, or should they all look like functions : would odd? or even? be a correct submission? \$\endgroup\$ Commented Mar 22, 2017 at 21:53
  • 1
    \$\begingroup\$ @EricDuminil Correct; in ruby 0 and 1 are both truthy. \$\endgroup\$ Commented Mar 23, 2017 at 20:11
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\$

Julia 0.5, 5 bytes

isodd

Try it online!

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

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!

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

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.
\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 23, 2017 at 15:29
  • \$\begingroup\$ This seems much shorter, and Taxi answers are generally similar so thats natural. \$\endgroup\$ Commented Mar 23, 2017 at 15:33
  • \$\begingroup\$ @SIGSEGV Wow the Taxi website is back online. \$\endgroup\$ Commented Mar 23, 2017 at 15:49
  • \$\begingroup\$ You can save 3 bytes by removing the double quotes since the plan names don't have spaces. \$\endgroup\$ Commented Jan 10, 2022 at 19:09
3
\$\begingroup\$

K (oK), 2 bytes

2!

Try it online!

\$\endgroup\$
1
2
3 4 5
9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.