# Even and Odd Numbers

Sandbox

Adapted from exercise 4 of 100 little Keg exercises

The ability to repeat chunks of code within a program is an important part of any programming language. Also just as important is the ability to make decisions based on different values.

## The Challenge

I want you to write a program that:

• Starts at the number 0, and:
• For each number from 0 to 100 (inclusive):
• If the number is even, print the letter E (uppercase or lowercase)
• Otherwise: print the number

Keeping in the spirit of this challenge, your source code must fit the pattern of having characters with an odd ordinal value followed by characters with an even ordinal value. Note that ordinal value is defined here as if I called python's ord() function on the character using your language's prefered code page. In other words, the code points of your program should be as such:

Odd Even Odd Even Odd Even ...


or

Even Odd Even Odd Even Odd ...


More concisely, the code points of your program must alternate between being odd and even.

## Example Output

E 1 E 3 E 5 E 7 E 9 E 11 E 13 E 15 E 17 E 19 E 21 E 23 E 25 E 27 E 29 E 31 E 33 E 35 E 37 E 39 E 41 E 43 E 45 E 47 E 49 E 51 E 53 E 55 E 57 E 59 E 61 E 63 E 65 E 67 E 69 E 71 E 73 E 75 E 77 E 79 E 81 E 83 E 85 E 87 E 89 E 91 E 93 E 95 E 97 E 99 E


Output can be in any other convenient format with any other separator. Leading and trailing whitespace is acceptable.

## Script Validation (UTF-8 only)

Here is a Keg script to see if your program fits the parity pattern requirement

Note that this can only be used for languages that use utf-8 as their codepage.

Here is an alternate Haskell script written by @WheatWizard which can handle newlines and shows which characters are invalid.

Here is a Python Script that tells you where you need to insert extra characters. (As well as the binary codes and the ord code of the characters)

## Scoring

This is so the answer with the fewest amount of bytes wins.

Let it be known that flags don't count towards being in the pattern. They also don't count towards byte count in this challenge.

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

# Language Name, N bytes


where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes


If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes


You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes


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

• Why is this a decision problem? – xnor Dec 20 '19 at 6:25
• ... That's not what a decision-problem is. Decision problems check whether the input meets a certain criteria and outputs based on that – Jo King Dec 20 '19 at 10:49
• It should be noted that the Keg script in the question only checks the first line if you input more than one. – Wheat Wizard Dec 20 '19 at 19:52
• @Skoddie the code points of your program must alternate between being odd and even. I don't really see how that is hard to understand. Codepoints are the ordinal value of a character in an encoding (usually UTF-8). For example a space has the codepoint 32, which is even. Your code must alternate between odd and even, starting with either. This means that a lot of commonly used function names, like print, range or any of the same character in a row (&& or ||) cannot be used in your program, since they violate that rule – Jo King Dec 21 '19 at 7:44

# JavaScript (ES6), 42 bytes

Contains a literal Tab.

f= y =>y>98? ' E ':' E ' + -~y +f   (1 -~y )


Try it online!

# Python 3, 72 bytes

x="E";z= 1
while 	z	<101:	x=x+ ["E"	,	f' {z} ' ] [z%2] ;z=z+ 1
exit	(	x)


Try it online!

Just by coincidence, exit and while are both available. The rest is just the program separated by spaces and tabs to make it alternate.

I'm hoping it's forgivable to output as an exit code, since print is unavailable.

• I'm perfectly happy with output being given as an exit code. – lyxal Dec 19 '19 at 23:55
• @Jono2906 Great, that's good! – Matthew Jensen Dec 20 '19 at 0:07
• Output as exit code is in the list of standard I/O – frank Dec 20 '19 at 0:19
• this isn't an "exit code", it's printed to stderr - but that's also valid by our rules – Doorknob Dec 20 '19 at 2:10
• @JoelTrauger Did it still met restricted source condition? – val is still with Monica Dec 20 '19 at 18:55

# MATL, 14 bytes

69H!Vo:E q"c@y


Try it online!

MATL uses ASCII encoding. The code points of the source code are

54  57  72  33  86 111  58  69  32 113  34  99  64 121


### How it works

69    % Push 69. This is the code point of 'E'
H     % Push 2
!     % Transpose: does nothing to 2
V     % Convert to string: gives '2'
o     % Convert to double: gives code point of '2', which is 50
:     % Range. Gives [1 2 ... 50]
E     % Multiply each element by 2. Gives [2 4 ... 100]
% (Space:) Does nothing
q     % Subtract 1 from each element. Gives [1 3 ... 99]
"     % For each k in [1 3 ... 99]
c   %   Convert to char. In the first iteration the top of the stack contains 69,
%   which is converted into 'E'. In subsequent iterations the top of the stack
%   contains 'E', which is left as is
@   %   Push current k
y   %   Duplicate from below: pushes another copy of 'E'
% End (implicit)
% Display stack, botom to top (implicit)


## Ruby, 41 bytes

n= 1
eval'puts"E";p	n;n=n+2; '*50;puts"E"


Try it online! (with parity check)

This builds a string that contains the code to output "E" and n, then add 2 to n. Then it multiplies the string by 50 (repeating it 50 times) and evals it. Finally, it prints the last "E" at the end.

The code produces a string as in the example.

[ c|s<-"E 1 E 3 E 5 E 7 E 9 E"	:[ ' ':k:m:' ':	"E"	|k<-"123456789"	,m<-"1 3 5 7 9"	,m>' ' ],c<- s ]


I was not able to find a way to print an integer or convert it to string. This is why the solutions looks like this.

Try it online!

# 05AB1E, 1511 9 bytes

т Ýxт>çÞ‡


-2 bytes thanks to @Grimmy.

Outputs as a list with lowercase e.

Try it online.

Explanation:

Shortest base version I could find was 8 7 bytes: тÝx'EÞ‡ (Outputs as a list with uppercase E.)

-1 byte thank to @Grimmy.

т        # Push 100
Ý       # Pop and push a list in the range [0, 100]
x      # Push a list with each value doubled (without popping the original list)
'E   '# Push "E"
Þ   # Pop and push an infinite list of "E": ["E","E","E",...]
‡  # Transliterate each doubled number to "E" in the original list
# (after which this list is output implicitly as result)



The codepoints of this 7-byter are [15,221,120,39,69,222,135] / [1,1,0,1,1,0,], which is already pretty good.

So I now have this:

т          # Push 100
# No-op space
Ý        # Pop and push a list in the range [0, 100]
x       # Push a list with each value doubled (without popping the original list)
т      # Push 100
>     # Increase it by 1 to 101
ç    # Convert this integer to a character with this ASCII codepoint: "E"
Þ   # Pop and push an infinite list of "e": ["e","e","e",...]
‡  # Transliterate each doubled number to "e" in the original list
# (after which this list is output implicitly as result)


This 9-byter has the codepoints: [15,32,221,120,15,62,231,222,135] / [1,0,1,0,1,0,1,0,1].

• Here's a shorter base: тÝx'EsǝJ. Also 8 bytes: тÝx'EÞ‡J. – Grimmy Dec 20 '19 at 16:13
• Here's a 10 that complies with the source restriction but outputs lowercase es. – Grimmy Dec 20 '19 at 16:44
• @Grimmy Nice. I've asked OP if a lowercase e (or an output as list) is allowed. I'll wait for his reaction before changing it. Thanks, though! :) – Kevin Cruijssen Dec 20 '19 at 16:47
• Lowercase is now allowed. Golf away! – lyxal Dec 21 '19 at 2:52
• The challenge already says it can be in any other convenient format so yes, you can. – lyxal Dec 21 '19 at 21:20

# Jelly, 13 bytes

³Ż¹⁾E Ḥ‘{$¦ K  Code-points: [131, 210, 129, 142, 69, 32, 175, 252, 123, 36, 5, 32, 75] A full program, which given no arguments prints the result. Try it online! (As a bonus given a non-negative integer argument it will print the sequence for that number, e.g.) ### How? ³Ż¹⁾E Ḥ‘{$¦ K - Main Link: no arguments
³             - literal 100 (if there are arguments provided, this is the 1st)
Ż            - zero-range = [0,1,2,3,...,100]
¹           - identity (no-op)
¦   - sparse application...
$- ...to indices: last two links as a monad: Ḥ - double = [0,2,4,6,...,200] { - use left argument as input to: ‘ - increment = [1,3,5,7,...,201] (note Jelly uses 1-based indexing) ⁾E. - ...what: literal character pair = ['E', ' '] . - no-op K - join with spaces - implicit, smashing print  # C (gcc) with -trigraphs flag, 161 141 bytes Thanks to WheatWizard for the validation script, which revealed that I mistakenly identified line endings as CR instead of LF! The fix didn't change the byte count, however. To get this to even work, in addition to the many spaces and tabs, I used a macro that uses digraphs and token-pasting to generate the disallowed function names (main and printf). As Unix line endings are LF (decimal 10), I had to add a space at the end of the first line to continue the odd/even pattern. Saving some more space, I could coalesce in because the two letters fit the odd/even pattern. Parity verification #define y(a,c,g,i ) a %:%:c %:%: in%:%:g %:%:i b; y(m,a, , )() {for (; b <101 ;b = b+ 1 ) y( p , r , t , f)( b%2?"%d " : "E " , b) ; }  Try it online! # Runic Enchantments, 32 bytes >'D1+k$1:/$'D1+k$/2+:1.C(1+?.;8?
EOEOEOEOEOEOEOEOEOEOEOEOEOEOEOEO


Try it online!

EO sequence above just indicates whether each byte is even or odd.

# Jelly, 18 bytes

ØA 5ị¶ 101Ḷ× Ḃ$oÑK  Try it online! In the Jelly code page, these character have the byte values of 12 41 20 35 D8 7F 20 31 30 31 B2 11 20 BF 24 6F 10 4B  which follows the pattern Even Odd Even Odd ... ## How it works Removing the extra spaces we get: ØA5ị 101Ḷ×Ḃ$oÑK


which is:

ØA5ị       - Helper link: Yield "E"
ØA         - Yield the upper case alphabet
5ị       - Take the fifth element ("E")

101Ḷ×Ḃ$oÑK - Main link: Yield "E 1 E 3 ... E 99 E" 101 - Yield 101 Ḷ - Lowered range: [0, 1, 2, ..., 99, 100] Ḃ$    - Take the parity of each:  [0, 1, 0, ..., 1, 0]
×      - Multiply each together:   [0, 1, 0, 3, ..., 99, 0]
Ñ  - Call the helper link:     "E"
o   - Replace the 0s with "E"s: ["E", 1, "E", ..., 99, "E"]
K - Join with spaces:         "E 1 E 3 ... E 99 E"


# Pyth, 16 bytes

V101p? %N/T5NK\E


Try it online!

Verify it online!

Separated by ""

## How it works:

V101p? %N/T5NK\E
V101                  - For 'N' in 0..100
? %N/T5        - If N modulo (10÷5) (or N%2) is truthy...
N       - ...return N
K\E    - Otherwise, return 'E' (K here is just a separator)
p               - Print the result


# R, 35 bytes

Thanks to ErikF for correcting a bug.

x=0	:{ 98+2} ;x[ c(	T	,!T) ] ="E";x


Try it online!

Defines x as the vector of integers from 0 to 100, then replaces all the even values with "E" and outputs x.

Previous, quite different version:

# R, 42 bytes

for	(i in	 1:50) write(c(i*+2- 1,	"E"),1 )


Try it online!

Includes a few tabs instead of spaces. Uses newlines as separators.

cat and print are unusable, but luckily write is OK. I had to resort to i*+2 -1 instead of i*2-1 to keep the alternation.

• Ahh right my bad – niko Dec 22 '19 at 17:24

# Python 3, 65 bytes

exit	([ ["E"	,	j] [j%2]for	j	 in	 eval	('ran' +"g"+"e")(101 ) ] )


Try it online!

This submission was built on using tabs and spaces, as well as a Very Clever™ abuse of eval.

Edit: Fixed program to adhere to (I had an extra space by mistake).

# MathGolf, 20 bytes

"E"q♀{î♥☻/÷i¿{"E"}îq


Try it online!

Very similar to the FizzBuzz example. Uses the MathGolf codepage, can verify parity with this script.

"E"q                # Push and output "E"
♀               # Push 100
{              # Loop from 1 to 100
î             # Push loop counter
♥☻/          # Push 32, 16, and divide (basically pushes 2)
÷          # Is the loop counter divisible by 2?
i         # Convert to integer (does nothing)
¿{"E"}   # If so, push "E"
î  # If not, push the loop counter
q # Output


# Jelly, 15 bytes

101Ḷ¹⁾E Ḃ ?€Ḣ€K


A full program that takes no argument and prints the desired output. The footer demonstrates both the parity and result.

# Ruby, 50 bytes

Adaptation of the Python answer. Unlike Python, however, Ruby puts is valid code to use for output, eliminating the need for exit hacks.

I didn't want to do a direct port, but map, each, upto, and times are all forbidden by the spec...

x=0;(	puts ["E"	,	x] [x%2] ;x=x+ 1 ) while 	x	<101


Try it online!

# Japt, 15 bytes

ò#d_v) ?"E"):»Z


Test it

ò#d               range [0-100]
_              passed through
v) ?          divisible by 2?
"E"):»Z   replace with 'E' else replace with number


Layout : [242,35,100,95,118,41,32,63,98,69,98,41,58,187,90]

w = k 1
a(c:e ) = c
i =(id)
e a =(a:)
m 1 =(a"1")
m 3 =(a"3")
m 5 =(a"5")
m 7 =(a"7")
m 9 =(a"9")
m c|c<1 = '0'|c<3 = '2'|c<5 = '4'|c<7 = '6'|c<9 = '8'
m c =(m$c - 10) q 1 =0; q c|m c<a"1"= 1 + q(c - 1 ) q c = q$c - 1
k c|c>98= e(a"E")$e(a"9")$e(a"9")$i"E"; k c = e(a"E")$e(m$q c )$e(m c )$k$c +2


Try it online!

This was pretty hard since show and print are both unusable, so I had to convert integers to strings on my own.

## Wz, 19 bytes

10#0#.a2m a&#"E"#|M


Pretty much the same as below. 100.a2ma&"E"|M The only difference is the 0-range (which does no good for the program).

## W, 19 bytes

101 a2m"E"#&a 1 -|M


## Explanation

Pretty simple: # and are both readability characters here and have no result on the stack.

101a2m"E"&a1-|M

101           M % Foreach the range 1 -> 101
a2m          % Modulo the current item by 2
"E"&      % If true, return "E" (Because odd-1 -> even)
a1-|  % Otherwise, return a-1


# GolfScript, 18 bytes

I didn't expect such a small amount of whitespace ...

101,{.3(%\"E"if} %


Try it online!

## Explanation

101,               # Generate range [0 1 2 ... 99 100]
{          } % # Map every item using this scheme
.             # Copy the item
3(%          # Modulo by 3-1 (2)
\         # If modulo-ing by 2 is truthy: return the item
"E"if    # Otherwise, return "E"

• 1{"E"\.2+}51*;; does what you did but 2 byes shorter (15, since the space before % isn't needed). – Mathgeek Feb 26 '20 at 18:01

# PowerShell, 72 bytes

fort(t$i =0;$i -le 101 - 1 ;$i =$i + 1 ) {$ab=(' e ',t$i ) ;$ab[$i %2] }


For testing:

$a = "fort(t$i =0;$i -le 101 - 1 ;$i =$i + 1 ) {$ab=(' e ',t$i ) ;$ab[$i %2] }" # Even/odd ok? 0..($a.Length-1) | % { [char]$a[$_] + "  " + ([byte][char]$a[$_] % 2)}
$a.Length Invoke-Expression$a


Missed the even-odd part in the code, thanks for pointing it out!

Try It Online

# Charcoal, 24 18 bytes

Ｆ¹⁰¹«¿⊗﹪⊕ι²E↥Ｉ⌈ι⁰→


Try it online! Link is to verbose version of code. Here are the 18 hex codes in Charcoal's code page:

C6 B1 B0 B1 A8 BF 9E A5 9C E9 B2 45 18 C9 1A A9 B0 13


Explanation:

Ｆ¹⁰¹«


Loop from 0 to 100.

¿⊗﹪⊕ι²


Test whether the index is even. The index is incremented to comply with the source code restriction, which reverses the sense of the test. The result of the test is doubled to comply with the source code restriction, but this doesn't change the outcome of the decision.

E


If the number was even then output an E.

↥Ｉ⌈ι


Otherwise take the ceiling of the number to comply with the source code restriction, cast the result to string, and uppercase the result to comply with the source code restriction.

⁰


Print 0 -s to comply with the source code restriction.

→


Leave a space between successive outputs.

Note: If a trailing space is acceptable, then the last two characters can be replaced with a space for 17 bytes, although there are alternative solutions, such as this:

Ｆ¹⁰¹«¿⊗﹪⊕ι²E ⁺⌈ι


Try it online! Link is to verbose version of code. Here are the 17 hex codes in Charcoal's code page:

C6 B1 B0 B1 A8 BF 9E A5 9C E9 B2 45 20 AB 1A E9 20


Explanation: Much like the above, except that in the even case we directly include the space in the literal while in the odd case the ceiling of the number is concatenated with a space.

# Zsh, 50 30 34 bytes

Bug fix: I was checking each pair of letters to make sure it had a character of each parity, instead of ensuring it alternated.

"e"cho E"	"{"1"'.'.9"9"'.'.'0'2} E


Try it online! Try it online! Try it online! Self-tests. Contains a literal tab.

I couldn't figure out a way to finagle the most straightforward echo E\ {1..99..2} E into anything, mostly due to the double period. EDIT: I did it. Turns out in normal expressions, quotes inside brace ranges don't really matter.

# Pip, 16 bytes

1 +2	*	,50	JW"E"


Try it online!

Outputs without separators.

Spaces and tabs are no-ops, so this boils down to:

    ,50       Range(50)                                               0, 1, ..., 49
2*          Multiply each by 2                                      0, 2, ..., 98
1+            Add 1                                                   1, 3, ..., 99
JW"E"  Join with "E", wrapping the outside with "E"s as well   E1E3E...E99E