| Bytes | Lang | Time | Link |
|---|---|---|---|
| 033 | SAKO | 250319T111344Z | Acrimori |
| 021 | Nim | 250325T032204Z | janAkali |
| 018 | なでしこ3 | 250304T095417Z | 鳴神裁四点一号 |
| 005 | Kona | 250116T150106Z | Abstract |
| 019 | Python3 | 250115T234940Z | Laszlo P |
| 040 | Bespoke | 250115T190100Z | Josiah W |
| 008 | BrainChild ASM | 250115T042007Z | ATaco |
| 005 | AWK | 241205T022723Z | xrs |
| nan | 240810T144910Z | RARE Kpo | |
| 005 | Uiua | 240810T135356Z | Joonas |
| nan | 230701T040616Z | Dadsdy | |
| 020 | sed | 230701T200218Z | Yelp |
| 010 | Desmoslang | 230530T044133Z | Dadsdy |
| 001 | Thunno 2 | 230528T141727Z | The Thon |
| 039 | Swift Compiler | 230515T235808Z | Bbrk24 |
| 041 | TypeScript's Type System | 230502T133229Z | Tristan |
| 010 | C gcc | 230502T152431Z | evanstar |
| 001 | GBZ80 machine code | 230415T224314Z | SNBeast |
| 006 | minigolf | 230415T131645Z | user1176 |
| 009 | GolFunc | 230326T132236Z | Joao-3 |
| nan | BotEngine | 190327T185728Z | SuperJed |
| 005 | x86_64 Linux machine language | 170328T062630Z | ceilingc |
| 006 | Pascal | 221023T092608Z | Kai Burg |
| 002 | K ngn/k | 220506T143015Z | oeuf |
| 006 | WolframAlpha | 170910T042045Z | Matheus |
| 009 | Excel VBA | 220504T050542Z | Taylor R |
| 004 | dc | 220504T031654Z | scorpica |
| 012 | Desmos | 220315T160918Z | Not A Ch |
| 109 | KonamiCode | 211203T150033Z | Ginger |
| 009 | !@#$%^&*_+ | 200824T140439Z | Twilight |
| 074 | dotcomma | 200824T022450Z | rydwolf |
| 013 | Headascii | 220204T203926Z | thejonym |
| 014 | Python REPL | 220130T165511Z | Oliver |
| 151 | LOLCODE | 220201T190718Z | Ginger |
| 011 | APL Dyalog Unicode | 220113T153526Z | Larry Ba |
| 015 | HP 48 User REPL | 220104T013121Z | TheSlive |
| 005 | Add++ | 220103T161943Z | Fmbalbue |
| 229 | INTERCAL CINTERCAL | 210925T142606Z | user1004 |
| 039 | INTERCAL | 210925T124111Z | user1004 |
| 016 | Python 3 | 210922T185757Z | Larry Ba |
| 001 | Vyxal | 210607T072303Z | emanresu |
| 006 | Duocentehexaquinquagesimal | 210416T175251Z | Makonede |
| 007 | Regex ECMAScript | 190313T063201Z | Deadcode |
| 004 | Factor | 210317T232444Z | user |
| 006 | Stack Cats + mn | 210316T171808Z | Aiden4 |
| 024 | Python | 210315T193314Z | SegFault |
| 004 | ARM Thumb2 machine code | 210216T073220Z | EasyasPi |
| 019 | Kotlin | 210210T183935Z | grian |
| 003 | Bound | 201014T164059Z | HoofedEa |
| 005 | sed | 200924T212250Z | Chris |
| 040 | 17 | 180204T202234Z | Hugh Wil |
| 004 | Check | 170617T064205Z | Esolangi |
| 006 | Aceto | 170508T082931Z | Laura Bo |
| 001 | 05AB1E | 200924T003104Z | Makonede |
| 047 | Rockstar | 200923T112007Z | Shaggy |
| 010 | Flurry nii | 200814T022907Z | Bubbler |
| 009 | Google Sheets | 200825T212250Z | Nithin D |
| 002 | Arn | 200824T020627Z | ZippyMag |
| 015 | Python 3 | 200811T063341Z | Eesa |
| 009 | MAWP | 200811T052535Z | Dion |
| 036 | MAWP | 200810T133529Z | Razetime |
| 021 | FEU | 200710T103345Z | PkmnQ |
| 029 | SystemVerilog | 200709T235215Z | user9468 |
| 040 | Microsoft Word Math Autocorrect | 200709T115456Z | General |
| 092 | Pyramid Scheme | 200708T070616Z | Jo King |
| 025 | Brainetry w 2 | 200707T150924Z | RGS |
| 007 | asm2bf | 200420T100130Z | Kamila S |
| nan | Pyramid Scheme | 190729T222004Z | Khuldrae |
| 001 | 05AB1E | 190907T180634Z | Sagittar |
| 012 | Python 3 | 191224T010056Z | Sagittar |
| 006 | Symbolic Python | 181214T204759Z | FlipTack |
| 148 | Rust | 191216T041752Z | Matthew |
| 002 | W | 191215T073401Z | user8505 |
| 015 | Kotlin | 191215T041324Z | smac89 |
| 137 | Shakespeare Programming Language | 191114T161813Z | Hello Go |
| 190 | Poetic | 191116T090752Z | JosiahRy |
| 099 | Turing Machine Code | 190327T221948Z | SuperJed |
| 015 | BitCycle | 190326T122450Z | Jo King |
| 037 | MarioLANG | 191114T160607Z | CoedRhyf |
| 042 | Sinclair ZX81 BASIC 42 tokenized BASIC bytes | 170321T130309Z | Shaun Be |
| nan | Commodore BASIC C64/128 | 191114T132800Z | Shaun Be |
| 004 | RPL | 191114T130726Z | Nacre |
| 006 | Toi | 191003T082607Z | user4180 |
| 006 | Ruby | 190925T145401Z | AJFarada |
| 005 | Cascade | 190924T182206Z | Edgex42 |
| 007 | MineFriff | 190921T232523Z | lyxal |
| 007 | Redirection | 190921T193304Z | Edgex42 |
| 003 | Keg | 190810T095733Z | Edgex42 |
| 016 | AsciiDots | 190820T153330Z | Edgex42 |
| 010 | Underload | 190731T073903Z | Edgex42 |
| 149 | Pyramid Scheme | 190730T023338Z | Jo King |
| 008 | Scala | 190730T071854Z | user8827 |
| 023 | ZX80 4K ROM 23 tokenised BASIC bytes | 190512T205224Z | Shaun Be |
| 001 | Intel 8080 machine code | 190327T174025Z | 640KB |
| 068 | Come Here | 190327T223336Z | SuperJed |
| 004 | Microscript | 190327T193653Z | SuperJed |
| 003 | MATL | 170321T112118Z | Luis Men |
| 001 | Japt | 170321T132536Z | Oliver |
| 005 | Gol><> | 190325T134324Z | KrystosT |
| 019 | Tamsin | 190325T081402Z | Esolangi |
| 045 | INTERCAL | 190315T104830Z | Unrelate |
| 035 | Alchemist | 190125T235406Z | ბიმო |
| 031 | Little Man Computer | 181214T193919Z | FlipTack |
| 014 | Pepe | 180908T062341Z | u-ndefin |
| 008 | Z80Golf | 180813T003051Z | Bubbler |
| 022 | BitCycle | 170322T051618Z | DLosc |
| 022 | CHIP8 assembly | 180817T215957Z | Ciaran_M |
| 006 | Flobnar | 180817T064529Z | Esolangi |
| 013 | Python 2 | 180813T172729Z | Triggern |
| 005 | Ahead | 180813T071416Z | snail_ |
| 009 | Python REPL/Shell | 180601T172314Z | Khalil |
| 006 | JavaScript Node.js | 180606T192453Z | Luis fel |
| 020 | Eukleides | 180601T201146Z | brhfl |
| 020 | HadesLang | 180601T193844Z | Azeros |
| 099 | C gcc | 180601T025700Z | Marhtini |
| 017 | C gcc | 180601T125828Z | Windmill |
| 011 | Charm | 180601T011111Z | LyricLy |
| 019 | Tcl | 180601T005355Z | sergiol |
| 006 | Perl 5 | 180413T143149Z | Ton Hosp |
| 036 | ActionScript 2.0 | 180316T215208Z | Jhynjhir |
| 013 | Dodos | 180316T123401Z | Leo |
| 013 | SmileBASIC | 180313T172439Z | 12Me21 |
| 027 | SNOBOL4 CSNOBOL4 | 171214T202227Z | Giuseppe |
| 030 | Whitespace | 180313T122311Z | Kevin Cr |
| 010 | Piet | 180212T233051Z | mathmand |
| 013 | Momema | 180212T074816Z | Esolangi |
| 028 | Fortran 95 | 170321T191324Z | Steadybo |
| 010 | BrainFlak | 180201T092634Z | Jo King |
| 002 | Pyt | 180201T013222Z | mudkip20 |
| 005 | Underload | 180131T231241Z | Esolangi |
| 019 | newline | 171214T201107Z | user6318 |
| 005 | Aceto | 171213T211357Z | qqq |
| 004 | √ å ı ¥ ® Ï Ø ¿ | 170331T065710Z | caird co |
| 002 | Implicit | 170910T031425Z | MD XF |
| 008 | ES2018 | 170914T144606Z | user7422 |
| 006 | expr | 170910T053848Z | gdahlm |
| 001 | 170711T000020Z | totallyh | |
| 012 | Python | 170321T103922Z | penalosa |
| 001 | Neim | 170708T190337Z | dj0965 |
| 004 | Common Lisp | 170616T203602Z | Renzo |
| 040 | WC | 170616T200517Z | Jaggler3 |
| 006 | Triangular | 170615T163123Z | MD XF |
| 003 | braingasm | 170323T134730Z | daniero |
| 002 | Actually | 170520T014950Z | user4594 |
| 006 | JavaScript | 170415T200053Z | Dylan Tu |
| 009 | CasioBasic | 170520T013640Z | numberma |
| 008 | Java 8 | 170321T103743Z | Kevin Cr |
| 013 | REXX 13 BYTES | 170517T142043Z | theblitz |
| 163 | Klein | 170517T042733Z | Wheat Wi |
| 005 | Juby | 170517T054807Z | Cyoce |
| 020 | BrainFlak | 170321T150831Z | Wheat Wi |
| 021 | Python 3 | 170416T231435Z | Dat |
| 016 | Python 2 | 170331T150358Z | Amol |
| 039 | TIS100 | 170321T162759Z | Unlimite |
| 003 | x86 machine code | 170418T061621Z | user2330 |
| 005 | /// | 170417T224822Z | sporkl |
| 028 | Javascript <= ES5 | 170417T211850Z | user6828 |
| 003 | Convex | 170416T222922Z | GamrCorp |
| 010 | R | 170416T082325Z | user1159 |
| 002 | x86 machine code | 170415T220657Z | user5434 |
| 002 | Chip | 170414T204425Z | Phlarx |
| 007 | Alice | 170411T135232Z | Martin E |
| 3229 | Dogescript | 170323T013730Z | vpzomtrr |
| 009 | Taxi | 170321T140241Z | Engineer |
| 018 | Swift | 170331T070353Z | John McD |
| 004 | TIBasic83 | 170321T175815Z | Magic Oc |
| 008 | brainfuck | 170321T150128Z | Dennis |
| 015 | Clojure | 170322T232121Z | Carcigen |
| 003 | Scala | 170324T211930Z | 2xsaiko |
| 009 | Scala | 170327T144733Z | sprague4 |
| 007 | 7 | 170321T122728Z | user6213 |
| 014 | PHP | 170327T120922Z | Daniel W |
| 283 | ArnoldC | 170321T140123Z | Luis Men |
| 043 | Acc!! | 170327T034801Z | DLosc |
| 009 | Bash + bc | 170324T173322Z | Christop |
| 002 | J | 170326T230220Z | Richard |
| 039 | OIL | 170326T230737Z | L3viatha |
| 067 | LOLCODE | 170321T122225Z | devRiche |
| 020 | Lua | 170326T182516Z | Josh |
| 003 | Python REPL | 170326T142640Z | Aashutos |
| 068 | Funciton | 170326T151231Z | Martin E |
| 002 | Pyth | 170324T060056Z | Steven H |
| 014 | Scala | 170323T184655Z | Stefan A |
| 002 | K oK | 170325T124951Z | Dennis |
| 054 | Vim Macro 54 Bytes | 170325T050119Z | Stefan A |
| 010 | Elisp | 170324T175348Z | Christop |
| 008 | J | 170324T193232Z | Plasmati |
| 009 | R | 170321T125034Z | Rudier |
| 022 | BrainFlak | 170321T130419Z | Riley |
| 006 | Juby | 170323T174239Z | Cyoce |
| 008 | Gibberish | 170323T164656Z | Dead Pos |
| 797 | Taxi | 170323T152915Z | Erik the |
| 064 | Ook! | 170321T200057Z | seshouma |
| 006 | Beeswax | 170323T151850Z | Okx |
| 004 | QwertyRPN | 170323T151048Z | Okx |
| 022 | Haxe | 170323T125906Z | ETHprodu |
| 011 | C | 170323T094323Z | G B |
| 003 | APL Dyalog | 170323T100033Z | Adá |
| 005 | Carrot | 170323T092915Z | user4180 |
| 004 | TIBasic | 170322T144651Z | Timtech |
| 031 | ZX Spectrum BASIC | 170321T211548Z | Luis Men |
| 029 | Rail | 170322T143331Z | Martin E |
| 009 | Python 2 REPL | 170321T122147Z | Felipe N |
| 012 | PHP | 170322T134939Z | aross |
| 006 | Jellyfish | 170322T133315Z | Martin E |
| 003 | Haskell | 170322T110212Z | AlexJ136 |
| 008 | Campfire | 170322T094912Z | Leo |
| 003 | Pip | 170322T085924Z | steenber |
| 015 | QBasic 4.5 | 170321T121918Z | steenber |
| 019 | LibreLogo | 170322T051259Z | Grant Mi |
| 018 | tinylisp | 170322T044911Z | DLosc |
| 004 | Wise | 170322T030705Z | MercyBea |
| 012 | brainfuck | 170321T145115Z | Dennis |
| nan | 170321T235814Z | Brad Gil | |
| 041 | Scratch | 170321T224141Z | AAM111 |
| 029 | TacO | 170321T222024Z | ATaco |
| 6420 | JSFuck | 170321T175818Z | powelles |
| 002 | Jelly | 170321T185322Z | steenber |
| 011 | Forth | 170321T192641Z | mbomb007 |
| 011 | Bash + coreutils | 170321T185043Z | seshouma |
| 008 | Pure Bash | 170321T160154Z | Digital |
| 003 | J | 170321T181649Z | Bijan |
| 007 | Groovy | 170321T180326Z | Magic Oc |
| 010 | SQLite | 170321T181457Z | Magic Oc |
| 007 | GNU sed | 170321T144102Z | seshouma |
| 006 | V | 170321T162233Z | user4180 |
| 025 | C++ | 170321T180555Z | Not a re |
| 026 | Go | 170321T172734Z | powelles |
| 005 | Befunge 93 | 170321T173459Z | MercyBea |
| 004 | Befunge 98 | 170321T173906Z | MercyBea |
| 015 | yup | 170321T173702Z | Conor O& |
| 008 | Retina | 170321T105323Z | Martin E |
| 006 | Cubix | 170321T164220Z | Luke |
| 003 | Pyth | 170321T162258Z | Digital |
| 003 | Retina | 170321T104931Z | Neil |
| 026 | Applescript function | 170321T160820Z | Digital |
| 1516 | Piet | 170321T160253Z | Mike Buf |
| 009 | bc | 170321T155954Z | seshouma |
| 004 | dc | 170321T153813Z | seshouma |
| 005 | QBIC | 170321T121552Z | steenber |
| 009 | PowerShell | 170321T142853Z | AdmBorkB |
| 010 | Excel | 170321T142548Z | Magic Oc |
| 007 | Hexagony | 170321T141548Z | Martin E |
| 002 | Sesos | 170321T141357Z | Dennis |
| 437 | ><> | 170321T140856Z | PidgeyUs |
| 091 | Java | 170321T133500Z | user6333 |
| 005 | Julia 0.5 | 170321T133053Z | Dennis |
| 007 | Common Lisp | 170321T132726Z | MatthewR |
| 014 | PHP | 170321T131819Z | Business |
| 015 | C | 170321T130549Z | Steadybo |
| 004 | CJam | 170321T130425Z | Business |
| 006 | PARI/GP | 170321T125202Z | alephalp |
| 021 | AHK | 170321T123437Z | Engineer |
| 010 | Python | 170321T115037Z | Rod |
| 007 | Cardinal | 170321T122637Z | fəˈnɛtɪk |
| 003 | Brachylog | 170321T122013Z | Fatalize |
| 001 | Jelly | 170321T113445Z | user6213 |
| 008 | Haskell | 170321T114047Z | Antisthe |
| 006 | JavaScript | 170321T113234Z | insertus |
| 002 | Ohm | 170321T113515Z | Linnea G |
| 004 | Clojure | 170321T111132Z | cliffroo |
| 010 | Ruby | 170321T111027Z | G B |
| 012 | Octave | 170321T110620Z | Stewie G |
| 005 | jq | 170321T105850Z | manatwor |
| 014 | brainfuck | 170321T105805Z | Emigna |
| 008 | C# | 170321T104104Z | TheLetha |
| 005 | Labyrinth | 170321T104701Z | Martin E |
| 016 | Batch | 170321T104628Z | Neil |
| 004 | Mathematica | 170321T104251Z | Martin E |
| 006 | Perl 5 | 170321T104053Z | Dada |
| 001 | 05AB1E | 170321T103403Z | Okx |
SAKO, 38 33 bytes
1)LINIAMOD(CCC(.),2)
STOP1
KONIEC
Reads from STDIN, prints to STDOUT.
Prints carriage return if even or line feed if odd.
Nim, 21 bytes
proc(n=1):bool=n%%2>0
n=1 is a default argument, we save few bytes compared to n:int
%% is an unsigned modulo
Try it online! (with 6 byte header)
なでしこ3, 18 bytes
●(a)AND(a,1)💧
Bitwise AND. 1 if odd, 0 otherwise.
Test suite and demo
<input id=i type=number></input><p id=p>{{Result}}</p>
<script type=なでしこ>
f=●(a)AND(a,1)💧
●テスト
f(1)と1がテスト等しい
f(2)と0がテスト等しい
f(16384)と0がテスト等しい
f(9999999999)と1がテスト等しい
「テスト成功」と表示
💧
テストする。
結果は「p」のDOM要素ID取得
「i」のDOM要素ID取得の変更時にはiからテキスト取得し結果にf(それ)をテキスト設定💧
</script>
<script src="https://nadesi.com/v3/cdn.php?v=3.7.2&f=release/wnako3.js&run"
></script>
Kona, 5 Bytes
{x!2}
0 if even, 1 if odd. Similarly, though it is 6 bytes,
{~x!2}
outputs 1 if even, and 0 if odd.
Python3 19 bytes
not(int(input())%2)
Bespoke, 40 bytes
could i EVEN do anything
indeed,nODDed i
Bespoke is a new esoteric programming language I created near the start of 2025. It's similar to my earlier esolang Poetic, in that word lengths are converted to instructions. This time, however, the instructions are part of a stack-based language, with arbitrary-precision signed integers and loop breaking/continuing and functions and everything!
Here is this program in "pseudocode" (which is able to run as Bespoke code - in fact, it's equivalent to the given code!):
INPUT N
PUSH BI
STACKTOP MODULO
OUTPUT N
This simply calculates the input modulo 2.
BrainChild ASM, 8 bytes
asm function evenorodd(int) -> int {
cpyab
cmp
incb
incb
xpopa
divab
xpushb
ret
}
Hex-dump of bytecode
0 1 2 3 4 5 6 7 8 9 A B C D E F
00000000: 04 2B 16 16 34 23 2F 14 | .+..4#/.
Takes a number from the x stack and places back 1 for odd, or 0 for even.
cpyab,cmp,incb,incb works out 1 byte shorter than setb 2 to set the b register to 2 for the div instruction, and the b register is unfortunately not guaranteed to be empty at the start of the function call.
what about just
1&++n
or maybe
~n&1
I presume ( ) for the 2nd version is superfluous since ~(n&1) and (~n)&1 should result in the same thing logically.
The safe way to write that maybe 1&~n instead.
(,) ds, 16 14 Chars or \$14\log_{256}(3)\approx\$ 2.77 Bytes
((()),,((())))
Accepts input in unary as a sequence of null bytes (easiest way to make it is by adding %00 to the input section in the URL), and outputs -1 for odd and 0 for even.
Thunno 2, 1 byte
E
Attempt This Online! Uses \$1\$ (true) for even, and \$0\$ (false) for odd.
ɗ
Attempt This Online! Uses \$0\$ (false) for even, and \$1\$ (true) for odd.
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()
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>>()
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.
TypeScript's Type System, No Ignore, 400 41 Bytes
type F<N>=N extends[...any,0|2|4|6|8]?1:0
- Use Jacob's suggestion of checking the last number.
Usage
// input is taken as an array (12 -> [1, 2])
type Test = F<[1,2]> // 1, the number is even
type Test2 = F<[1,1]> // 0, the number is odd.
The program checks if the last digit in the array is an even number.
GBZ80 machine code, 1 byte
Given an input in register A, this outputs in the carry flag such that 1 is odd and 0 is even.
0F
Disassembly:
rrca
This instruction rotates register A right once, but with the carry flag treated as bit -1 only for the right end. This instruction can be confused with rrc a, which is a two byte (CB 0F) instruction using a genericized pattern for other registers and which updates the zero flag.
Bit 0 determines parity. Performing a rotation right through carry puts bit 0 in the carry flag.
minigolf, 6 bytes
0i,0=_
Explanation
0 0, Initial output: 0 _
i, 0, Repeat input times: _
0= 0, Logical not (is it equal to 0?) _
_ 0, End repeat _
0, Implicit output TOS _
GolFunc, 9 bytes
.O..%.JN2
Outputs 1 for odd inputs and 0 for even inputs. (there will soon be an interpreter, but this works for now)
Explanation:
.JN # take input as a number
..% 2 # mod 2
.O # and then output.
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) );
}
}
EDIT: Fixed TIO link.
Pascal, ≥ 6 B
odd(x)
where x is an integer variable or expression returns the Boolean value true if x mod 2 = 1, otherwise false.
WolframAlpha, 6 bytes
x mod2
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
Note: Byte count in UTF-8 counted using this resource.
Excel VBA, 9 bytes
An anonymous immediate window function that bit masks all but the lowest bit of input from range [A1] and outputs to the immediate window
?1And[A1]
dc, 4 bytes
?2%p
Uses 1 for odd and 0 for even.
Explanation
This is just straightforward modulo.
? push input onto stack
2 push a 2 onto stack
% take modulo of input and 2
p print
Desmos, 12 bytes
mod(i,2)
i=0
1 is falsey odd number and 0 is truthy even number.
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.]
>(^^)
<<<
!@#$%^&*()_+, 14 9 bytes
^(1*1*)+#
(1 represents character \x01.)
Someone created an Esowiki page for this language and put a badly-golfed parity program there, which motivated me to create this program. It inputs n and inverts 0 n times.
I only now noticed I made an answer for this challenge, so I decided to golf it. This is a straightforward port of Dennis' BF answer. Input is unary with the character \x00. Output 1 if it's odd.
Find this unsatisfying? Here's output 1 if it's even, in still 9 bytes.
^(2**)+*#
dotcomma, 74 bytes
[[,.][.][.].,].[[[[,.][[].[],][[].[],].,][[,][[].[],][[].[],].],[],.][.].]
Dotcomma is a language I recently invented, designed to do things in an interesting way with as few instructions as possible. In order to accomplish this, there are two operators (predicatably . and ,) which can do entirely different things depending on context. This answer's explanation will be a somewhat high level explanation of how this program works. To see some simpler examples, check out the examples in the page linked in the title.
The first part of this program, [[,.][.][.].,], adds 2 to the inputted number. The [,.] block will take a number from the queue (which will contain the input), add it to two [.] blocks (equal to 1 each), and put it back in the queue. The . between the two main blocks creates a loop.
To make the loop easier to understand, here's the code broken into more manageable pieces:
[[,.][.][.].,].[
[
[[,.][[].[],][[].[],].,]
[[,][[].[],][[].[],].],[],.
][.].
]
The first part of the loop, on line 3, will take the current number from the queue and subtract 2 from it. It then puts it back in the queue, and the second part tests whether that number minus 2 is negative. If so, the loop ends.
The result is the number in the queue being either 0 if the input was even, or 1 if the input was odd.
Headascii, 20 13 bytes
{UN()}:R--O<E
Try it here! Code will have to be copied and executed like this:
erun("{UN()}:R--O<E","input here")
Outputs two differing error messages. For odd inputs:
error 0:12 E [ 1, 0, 0, 1 ] (1)
'Error: E index out of range'
For even inputs:
error 0:12 E [ 1, 0, 0, 1 ] (2)
'Error: E index out of range'
Explanation:
{UN()}:R--O<E Full program (Block 0)
{ } Loop, which
U Discard(s) input byte(s)
N() : Until there is no input
N( (and stores 1 in the comparison register)
R Recall the last byte
--O Subtract 2 and save
< If this number is less than the comparison register (1)
Return the number in the comparison register (1)
Else
Return 0
E Go to the code block corresponding to this number.
If it is block 0, the program loops to the beginning
with the saved byte as "input"
If it is block 1, the program will exit with an error,
as there is no block 1 in this program. The error message
will include register state information, including the
currently recall-able byte. In this state of the program,
odd inputs will have reached 1, and even inputs will have
reached 2.
I still think this is further golfable, but I'm proud of this clever little -7
Old solution (20 bytes):
{UN)}:R--O(<)[E:+()?
Try it here! Code will have to be copied and executed like this:
erun("{UN)}:R--O(<)[E:+()?","input here")
Prints a Headascii debug message if odd, and nothing if even.
Explanation:
{UN)}:R--O(<)[E:+()? Full program
{ } Loop which
U Discard(s) input byte(s)
N) : Until there is no more input
R Recall the last byte (as a number)
--O Subtract 2 and save
(<) If the number is greater than or equal to 0
[E Return to start of program, with saved number as input
: Else
+() If the number is -1
? Print a debug message
Repeatedly subtracts 2 from the last digit until it is -1 or -2. There might be a way to shave off a byte or two by switching the conditions / thens / elses around a bit. Just want to submit for now
Python REPL, 14 bytes
int(input())%2
Where 0 (falsy) is even, and 1 (truthy) is odd.
LOLCODE, 151 bytes
HAI 1.2
I HAS A N
GIMMEH N
N IS NOW A NUMBAR
I HAS A V ITZ QUOSHUNT OF N AN 2
VISIBLE MAEK BOTH SAEM DIFF OF V AN MAEK V A NUMBR AN 0.5 A NUMBR
KTHXBYE
HP 48 User REPL, 15 bytes
« 1 + 2 MOD
»
French quote marks mark the var as a program
'1 +' - adds one to the number on the stack (input)
'2 Mod'- returns 0 when odd, 1 when even
1 is a truth in RPL and 0 is false, so we have to shift it up once to make it valid.
On the calculator this is 26 bytes despite the source being 15 including nonessential formatting.
INTERCAL (C-INTERCAL), 229 bytes
This version accepts any integers, in decimal (e.g. -156); requires to be terminated with EOF.
Outputs _ for even and I for odd.
DO,1<-#1DOCOMEFROM#3DOWRITEIN,1(1)DO.1<-,1SUB#1DO.2<-.4DO(1000)NEXTDO.4<-.3PLEASE.3<-!3~#15'$!3~#240'DO.3<-!3~#15'$!3~#240'DO.2<-!3~#15'$!3~#240'PLEASE.1<-.5DO(1010)NEXT(3)DO.5<-.2PLEASECOMEFROM.1~#256DO.2<-.2~#128PLEASEREADOUT.2
How it works
- First part is taken from 229 bytes of cat program but without READing out character.
- In ASCII
0is 48,1is 49, ...9is 57. - But C-INTERCAL reverses bit order, so
0is 0x0c,1is 0x8c, ...9is 0x9c. - If bit at 0x80 is 1, then it's odd digit.
INTERCAL, 39 bytes
Domain is ZERO or OH to FOUR TWO NINE FOUR NINE SIX SEVEN TWO NINE FIVE. _ (zero) for even and I (one) for odd. Also works in CLC-INTERCAL.
DOWRITEIN:1DO:1<-':1~#1'PLEASEREADOUT:1
Regex (ECMAScript), 7 bytes
^(xx)*$
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 decision-problem function (with nonnegative integer input and match/no-match output) that requires the (, ), *, or + symbols.
Factor, 5 4 bytes
odd?
Outputs t if odd, f if even.
I feel a bit guilty doing this (and also stupid after Bubbler's suggestion).
Stack Cats + -mn, 6 bytes
[:^*_:
mirrored (done with -m flag):
[:^*_:_*^:]
Returns 1 for odd and and -1 for even. The n flag is used for numeric, rather than codepoint io.
Stack Cats is a very interesting language. Stack Cats performs operations on an infinite tape of bottomless stacks of arbitrary precision integers. It requires mirror symmetry, and most of the mirroring exactly undoes the previous operation. Despite this, Stack Cats is Turing complete, and that is most easily achieved by using a <(...)*(...)> construct that skips the entire first half. However, that construct would require more bytes than the approach I used. The first part [:^ moves the input to the top of the next stack and duplicates it. Since the inputs are terminated with -1 the stacks now looks like this:
[i]
... [-1] [i] ...
^
where i is the input. The next part *_ determines the parity of the input by subtracting the input from the input with its least significant bit flipped, leaving 1 for odd, or -1 for even. The stacks now look like this:
[r]
... [-1] [i] ...
^
where i is the input and r is the result. Next comes the hard part: ensuring the first half doesn't undo the second. That is done by a single : which flips the top two values of the stack, which now looks like this:
[i]
... [-1] [r] ....
^
_*^ then performs some operations on the input, leaving random garbage in its place, and the result unchanged. The final :] moves the result to a place it is printed (because the current stack is outputted, without any trailing -1s). The final stack looks like:
[r]
... [-1] [random garbage] ...
^
Python - 24 bytes
n=int(input())
print(n%2)
Hooray for built-ins! This program should be fairly easy to understand. Assumes n has not been inputted already into the program. If it has, only the second line is necessary, and this can be golfed down to 10 bytes.
ARM Thumb-2 machine code, 4 bytes
07c0 4770
.syntax unified
.arch armv6t2
.thumb
.globl is_odd
.thumb_func
// Standard C calling convention
// Input: uint32_t r0
// Output: r0 == 0 if even, else odd
is_odd:
// Shift out everything but the lowest
// bit, leaving odd numbers as
// 0x80000000 and even numbers as 0.
lsls r0, r0, #31
// Return
bx lr
Very simple. Shifts everything but the lowest bit into oblivion via lsls and returns.
Odd numbers will return nonzero and even numbers will return zero. It follows standard C calling convention.
0x01234567 << 31 = 0x80000000
0x12345678 << 31 = 0x00000000
You can't get any shorter than that.
For the reference, and{s} r0, #1 is a wide instruction.
Kotlin, 19 bytes
fun a(n:Int)=n%2==0
This was surprisingly simple. And the code is pretty self explanatory.
Bound, 3 bytes
i?.
Explanation:
i # Gets input and adds to stack if its an integer
? # Pops the top element and check if its even (0 is false, 1 is true)
. # Displays the stack
sed, 5 bytes
/0$/d
Takes the input as a binary string. sed does not have truthy/falsey values, so I am defining truthy as a non-empty output and falsey as an empty output. As per this meta post, this is acceptable since these sets are non-overlapping.
For the cost of one byte, we can instead output by exit code:
/0$/q1
The following also works if input is taken as a binary list of digits, linefeed separated, but I don't think that's an acceptable input method.
$!d
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.
0{I : a == 0 @}1{0 @ 2 % $$}777{0 0 @}
Check, 4 bytes
>2%p
Explanation: >2 pushes 2 onto the stack, % preforms modulus with the implicit input, and then p prints the result.
Aceto, 6 bytes.
%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.
05AB1E, 1 Byte
É
# (implicit) push STDIN to stack
É # push 1 if top of stack is odd, 0 if even
# (implicit) output top of stack to STDOUT
Rockstar, 47 bytes
listen to N
let D be N/2
turn up D
say D is N/2
Try it here (Code will need to be pasted in)
Flurry -nii, 14 12 10 bytes
{}{{}[]}{}
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
{}{{}(){}}{}
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
{}{{}[<><<>()>]}{}[<>()]
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,
succrepeated2ktimes givesn^(k+1) + n^k + ... + nsuccrepeated2k+1times givesn^(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
...
Google Sheets, 9 bytes
=MOD(A1,2
Google Sheets auto closes parenthesis. Its pretty self explanatory, it just does modulo 2 on the input (A1).
MAWP, 36 bytes
%@_1A[1A~25WWM~]%[{1A{1M}<0:.>}2A]1:
Explanation
It uses the same logic as all the other programs, but MAWP does not have a modulo operator.
The workaround is repeately deducting 2 from the number until we reach 0 or 1.
%@_1A[1A~25WWM~]% snippet used for inputting multi digit numbers
[ loop if top of stack != 0
{1A{1M}<0:.>} if number-1=0 print 0 and end program
2A] subtract 2 from the number
1: once loop ends, print 1
4 digit and higher numbers take very long to check in the online interpreter.
SystemVerilog, 29 bytes
task t(n);$write(n%2);endtask
Prints 1 for odd numbers and 0 for even numbers.
Testbench:
module m;
initial begin
t(1);
t(2);
t(16384);
t(99999999);
end
task t(n);$write(n%2);endtask
endmodule
Output (VCS on EDA Playground):
1 0 0 1
Microsoft Word Math Autocorrect, 40 bytes
Add the following entries to your Math Autocorrect table:
0$ -> $0
1$ -> $1
2$ -> $0
3$ -> $1
4$ -> $0
5$ -> $1
6$ -> $0
7$ -> $1
8$ -> $0
9$ -> $1
Then make a new equation. Separate each digit with a space, or this will not work. When you type your last digit, it will be replaced by either a $1 (if it's odd) or $0 (if it's even).
For example, to input the number 942, type 9 4 2$, which becomes 9 4 $1, telling me it's odd. As a bonus, this works for all even bases up to 10.
Pyramid Scheme, 108 92 bytes
^
/-\
^---^
-^ ^-
-^-
/^\
^---^
-^ /#\
/-\---^
/ 1 \ /l\
-----/ine\
-----
Outputs -2 for odd numbers and 0 for even ones. This can be swapped by switching the top pyramid for + instead of -. This is basically equivalent to (-1)**int(input) - (-1)**0
Brainetry -w 2, 25 bytes
Golfed version:
a b c d e f
a b c d e f g
How it works: we set the cell size to 2 with -w 2 which pretty much does all the """heavy""" lifting for us. After that we just input one number and output it. If you want to input an actual number instead of an ASCII code point you can also set the --numeric-io flag.
The golfed version was adapted from the program at the end of this answer. To try this online you can
- head over to this replit link, copy&paste the code into the
btry/replit.btryfile and hit the green "Run" button; - clone the github repo and from your terminal run
./brainetry btry/ppcg/even_or_odd.btry --numeric-io -w 2.
Let me read one input number
and now I will ouput its parity.
asm2bf, 7 bytes
modr1,2
Yes, it compiles. Takes input in r1, writes output to r1. 1 for odd, 0 for even.
Pyramid Scheme, 221 199 bytes
^ ^ ^
/ \ /l\-
/set\ /oop\
^-----^ ^-----^
- ^- -^ / \
^- /*\ /set\
/#\ ^---^-----^
^---/-\ - /-\
/l\ ^---^ ^---^
/ine\- /1\ - /2\
----- --- ---
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 (- n 1))))
(set! n (- n 2)))
(display n)
Python 3, 12 bytes
lambda n:n&1
Outputs 1 for odd, 0 for even.
Symbolic Python, 6 bytes
_&=_/_
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.
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.
Kotlin, 15 bytes
{n:Int->n%2<1}
Shakespeare Programming Language, 137 bytes
#.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.
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
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.)
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.
BitCycle, 19 17 16 15 bytes
?ABv
// <
!+\<
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
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
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
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.
Toi, 6 bytes
(r[rr]
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.
Ruby, 6 bytes
n.odd?
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.
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}
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.
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
AsciiDots, 16 bytes
.-#?{&}$#
.-#1/
Outputs 0 if even, and 1 if odd. Basically just a Boolean AND with 1.
Underload, 10 bytes
(0)(1)()^S
Input is in hard-coded in Unary, using ~ in the 3rd bracket. 0 for odd, 1 for even.
Pyramid Scheme, 185 149 bytes
^
/=\
^---^
^- /#\
^- ^---
/#\ /+\
---^---^
/"\ ^-
^---/"\
-^ ^---
//\-
^---^
-^ /2\
/#\---
---^
/ \
/arg\
^-----
-^
/1\
---
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.
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
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.
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:
Input = 2, light D0 is OFF indicating EVEN result:
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.
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]
Japt, 1 byte
v
Returns 1 for even numbers, 0 for odd.
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.
Gol><>, 5 bytes
I2%zh
take input, push 2, modulo a, b, then if the number was even, output 1, otherwise output 0
Tamsin, 19 bytes
main={"0"&"0"}&eof.
Takes input in unary. Recognizes an even number of 0 characters.
INTERCAL, 45 bytes
DOWRITEIN:1PLEASE:1<-:1~#1DOREADOUT:1DOGIVEUP
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.
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
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).
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
Z80Golf, 9 8 bytes
00000000: d5d2 0380 e601 ff76 .......v
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
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
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 :)
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 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.
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).
Flobnar, 6 bytes
&
%@
2
Try it online! (requires the -d flag)
I don't think it's going to get much shorter than this.
Python 2, 13 bytes
lambda n:~n&1
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.)
Python REPL/Shell, 9 bytes
input()&1
returns 0 for even and 1 for odd
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.
HadesLang, 20 bytes
raw:[in:[]] % 2 is 0
in:[] Reads from console
raw:[] Converts a string to any required datatype
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);}
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;}
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.
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;}
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.
Dodos, 13 bytes
main dip dip
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.
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.
SNOBOL4 (CSNOBOL4), 30 27 bytes
OUTPUT =REMDR(INPUT,2)
END
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?
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.
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
Piet, 10 codels
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):
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
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.
Brain-Flak, 10 bytes
({{}[{}]})
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.
Pyt, 2 bytes
2%
Explanation
Implicit input
2% Mod 2
Implicit print
Odd is truthy, even is falsy
Alternatively, also 2 bytes
2|
Explanation:
Implicit input
2| Is it divisible by 2?
Implicit output
Even is truthy, odd is falsy
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.
newline 19 bytes
g\n0\n/\n[\nd\n]
and the number 2
Aceto, 5 bytes
ri2%p
r reads input i converts to integer 2% takes the number modulo 2 p prints the answer
√ å ı ¥ ® Ï Ø ¿ , 4 bytes
Ik@o
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)
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 »;
ES2018, 8 Bytes
x=o=>o%2
Or, in ES5+typed arrays+intl:
ES5, 25 Bytes
function x(o){return o%2}
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 &
,,,, 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
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.
Common Lisp, 4 bytes
oddp
Builtin function, like Haskell odd.
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)
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.
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.
Actually, 2 bytes
1&
Output is 0 if even, 1 if odd. The code is very straightforward: bitwise-AND (&) with 1 (return low bit of input).
JavaScript, 6 bytes
A function of input n that returns 0 if even and 1 if odd.
n=>n%2
Casio-Basic, 9 bytes
mod(n,2)
8 bytes for the function, 1 to add n as an argument.
Java 8, 8 bytes
n->n%2<1
Java 7, 30 bytes
Object c(int n){return n%2<1;}
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;}
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:
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 @.
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.
Brain-Flak, 22 20 bytes
Here is annother cool answer in Brain-Flak you should also check out
(({})){({}[()]<>)}<>
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)
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.
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.
///, 5 bytes
/00//
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.
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}
R, 10 bytes
!scan()%%2
If you can get away with assuming n is the value, !n%%2 works.
x86 machine code, 2 bytes
00000000 a8 01 |..|
00000002
Sets the zero flag if the value in register A is even.
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
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
Alice, 7 bytes
1\@
oAi
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.
Dogescript, 32 29 bytes
such f much n
return n%2
wow
Returns 1 if n is odd, 0 otherwise.
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".
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.
Swift, 18 bytes
let f={n in n%2<1}
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.
brainfuck, 8 bytes
+[,>,]<.
Input is in unary. Output is the 1 (truthy) for odd numbers and NUL (falsy) for even numbers.
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.
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!
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
Scala, 9 bytes
(_:Int)%2
This outputs 1 for odd, 0 for even
7, 18 characters, 7 bytes
177407770236713353
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.
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.
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.
“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
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
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
J, 2 bytes
2| NB. remainder of arg / 2
So 0 = true (even), 1 = false (odd)
2| 7 => 1
2| 10 => 0
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.
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".
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.
Python REPL, 3 bytes
_&1
Explanation: Taking bitwise and with 1 gives True when even else False
Funciton, 68 bytes
Byte count assumes UTF-16 encoding with BOM.
╓─╖
║p║
╙┬╜┌┐
├─┤├
╔╧╗└┘
║1║
╚═╝
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)
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.)
Scala, 14 Bytes
(x:Int)=>x%2<1
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.
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!
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
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
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)
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
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
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.
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.
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!
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]
Haxe, 22 bytes
function(x)return x%2;
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.
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.
APL (Dyalog), 3 bytes
Full program body:
2|⎕
Alternatively:
2⊤⎕
| 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!
Carrot, 5 bytes
#^F%2
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
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.
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.
Rail, 29 bytes
$'main'
oi
r e
| >{main}
\2-
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/
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.
PHP, 12 bytes
echo$argn&1;
Run like this:
echo 123 | php -R 'echo$argn&1;';echo
- Odd: 1
- Even: 0
Haskell, 3 bytes:
odd
It's built-in. Works as expected. There is also even but of course that's one byte longer.
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.
Pip, 3 bytes
a%2
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.
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.
LibreLogo, 19 bytes
Code:
print int input "%2
Result:
Returns 0 for even numbers, and 1 for odd numbers.
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.
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)
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).
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.
Perl 6, 4 bytes
*%%2
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
Scratch, 4 blocks + 1 byte
Python equivalent:
i = input()
print(i % 2)
Returns 0 for even and 1 for odd.
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.
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()))
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.
Also at 2 bytes, the classical Modulo 2:
%2
which also returns 0/1 for even/odd resp.
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.
I use seq, because it is 1 byte shorter than echo, but it has a different truthy / falsy mapping.
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 $?
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
Groovy, 7 bytes
{!it%2}
Answer too short for posting.
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.
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.
V, 8 7 6 bytes
Àé1òhD
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 ò
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.
Befunge 93, 5 bytes
&2%.@
& 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
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.
yup, 15 bytes
{0e0:e---}00e--
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)
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.
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.
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
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
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!
Applescript function, 26
on f(i)
return i mod 2
end
Test calls
log f(1)
log f(2)
log f(3)
Piet, 15 codels / 16 bytes
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.
bc, 9 bytes
Output is 1 for odd, and 0 for even.
read()%2
A trailing newline is necessary to run successful, and it is included in the bytes count. The read() function works only for numerical input.
dc, 4 bytes
Output is 1 for odd, and 0 for even.
?2%p # read input, push 2, do modulo, print
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%
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)
PowerShell, 9 bytes
"$args"%2
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.
Hexagony, 7 bytes
?{2\%'!
Prints 0 for even numbers (falsy) and 1 for odd numbers (truthy).
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.
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.
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.
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.
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.
Common Lisp, 7 Bytes
#'evenp
Built-in function that checks parity. Barely beating Java!
C, 15 bytes
#define f(n)n&1
Returns 0 if even, 1 if odd.
PARI/GP, 6 bytes
n->n%2
Mod 2.
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.
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
Jelly, 1 byte
Ḃ
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
It's pretty short without the builtin, too. (This is bitwise-AND with 1.)
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
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.
Ohm, 2 bytes
è
Explanation:
è: (pop()%2)==1
Ohm also has implict input and output
Clojure, 4 bytes
odd?
A function which checks if its argument is odd. Example usage: (odd? 3). Clojure does also have even? function.
Ruby, 10 bytes
->x{x&1<1}
True for even, false for odd.
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.
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.
+>>,[-[->]<]<.
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;
Labyrinth, 5 bytes
?_2%!
Prints 0 for even and 1 for odd inputs.
? 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
?#&!@
? Read input.
# Push stack depth (1).
& Bitwise AND (extract least-significant bit).
! Print.
@ Terminate.
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
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?










![ask [] and wait; say ((answer) mod (2));](https://i.sstatic.net/tGnez.png)

