38
4
Introduction
In number theory, a number is considered evil if there are an even number of 1's in its binary representation. In today's challenge, you will be identifying whether or not a given number is evil.
Challenge
Your job is to write a full program or function which accepts a single, non-negative integer as input and outputs (or returns) whether or not that number is evil.
- You may output any truthy value if the number is evil, and any falsy value if the number is not evil.
- You may input and output in any acceptable format.
- Standard loopholes are disallowed.
- OEIS sequence A001969 is the sequence containing all evil numbers.
- Here is a list of the first 10000 evil numbers, for reference (and more test cases!)
- This question is code-golf, so the shorter, the better.
- Don't be put off by extremely short answers in golfing languages. I encourage you to submit in any language you like.
Here are some test cases:
3 => True 11 => False 777 => True 43 => True 55 => False 666 => False
The Leaderboard
At the bottom of the page is a stack snippet containing a leaderboard for this question. (Thanks, @MartinEnder)
To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:
# 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
/* Configuration */
var QUESTION_ID = 169724; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 81420; // This should be the user ID of the challenge author.
/* App */
var answers = [],
answers_hash, answer_ids, answer_page = 1,
more_answers = true,
comment_page;
function answersUrl(index) {
return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}
function commentUrl(index, answers) {
return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}
function getAnswers() {
jQuery.ajax({
url: answersUrl(answer_page++),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function(data) {
answers.push.apply(answers, data.items);
answers_hash = [];
answer_ids = [];
data.items.forEach(function(a) {
a.comments = [];
var id = +a.share_link.match(/\d+/);
answer_ids.push(id);
answers_hash[id] = a;
});
if (!data.has_more) more_answers = false;
comment_page = 1;
getComments();
}
});
}
function getComments() {
jQuery.ajax({
url: commentUrl(comment_page++, answer_ids),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function(data) {
data.items.forEach(function(c) {
if (c.owner.user_id === OVERRIDE_USER)
answers_hash[c.post_id].comments.push(c);
});
if (data.has_more) getComments();
else if (more_answers) getAnswers();
else process();
}
});
}
getAnswers();
var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;
var OVERRIDE_REG = /^Override\s*header:\s*/i;
function getAuthorName(a) {
return a.owner.display_name;
}
function process() {
var valid = [];
answers.forEach(function(a) {
var body = a.body;
a.comments.forEach(function(c) {
if (OVERRIDE_REG.test(c.body))
body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
});
var match = body.match(SCORE_REG);
if (match)
valid.push({
user: getAuthorName(a),
size: +match[2],
language: match[1],
link: a.share_link,
});
//else console.log(body);
});
valid.sort(function(a, b) {
var aB = a.size,
bB = b.size;
return aB - bB
});
var languages = {};
var place = 1;
var lastSize = null;
var lastPlace = 1;
valid.forEach(function(a) {
if (a.size != lastSize)
lastPlace = place;
lastSize = a.size;
++place;
var answer = jQuery("#answer-template").html();
answer = answer.replace("{{PLACE}}", lastPlace + ".")
.replace("{{NAME}}", a.user)
.replace("{{LANGUAGE}}", a.language)
.replace("{{SIZE}}", a.size)
.replace("{{LINK}}", a.link);
answer = jQuery(answer);
jQuery("#answers").append(answer);
var lang = a.language;
lang = jQuery('<a>' + lang + '</a>').text();
languages[lang] = languages[lang] || {
lang: a.language,
lang_raw: lang,
user: a.user,
size: a.size,
link: a.link
};
});
var langs = [];
for (var lang in languages)
if (languages.hasOwnProperty(lang))
langs.push(languages[lang]);
langs.sort(function(a, b) {
if (a.lang_raw.toLowerCase() > b.lang_raw.toLowerCase()) return 1;
if (a.lang_raw.toLowerCase() < b.lang_raw.toLowerCase()) return -1;
return 0;
});
for (var i = 0; i < langs.length; ++i) {
var language = jQuery("#language-template").html();
var lang = langs[i];
language = language.replace("{{LANGUAGE}}", lang.lang)
.replace("{{NAME}}", lang.user)
.replace("{{SIZE}}", lang.size)
.replace("{{LINK}}", lang.link);
language = jQuery(language);
jQuery("#languages").append(language);
}
}
body {
text-align: left !important;
display: block !important;
}
#answer-list {
padding: 10px;
width: 290px;
float: left;
}
#language-list {
padding: 10px;
width: 500px;
float: left;
}
table thead {
font-weight: bold;
}
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/all.css?v=ffb5d0584c5f">
<div id="language-list">
<h2>Shortest Solution 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>{{SIZE}}</td>
<td><a href="{{LINK}}">Link</a></td>
</tr>
</tbody>
</table>
<table style="display: none">
<tbody id="language-template">
<tr>
<td>{{LANGUAGE}}</td>
<td>{{NAME}}</td>
<td>{{SIZE}}</td>
<td><a href="{{LINK}}">Link</a></td>
</tr>
</tbody>
</table>
EDIT: I believe this question is not a duplicate of this, because whereas that question is asking to count the number of ones, this question is asking whether the number of ones is even. Although you can accomplish this question by simply counting the bits, there are other approaches too.
Sandbox. – Amphibological – 2018-08-01T15:09:30.457
2Related (XOR-ing every binary digit is the same as taking the sum modulo-2). – Kevin Cruijssen – 2018-08-01T15:25:03.913
4
Possible duplicate of Count the number of ones in an unsigned 16-bit integer
– Beta Decay – 2018-08-01T17:11:04.210My language (R) only has 32-bit signed integers; beyond that it switches by default to a
double
; is it OK if we run out of precision? – Giuseppe – 2018-08-01T19:56:08.203@Giuseppe no problem, as long as your program works for 32 bits you're fine. – Amphibological – 2018-08-01T19:56:57.850
@BetaDecay I have tried to address that above. I personally don't think this is a duplicate though. – Amphibological – 2018-08-01T20:01:23.017
This is a dupe because you can take any of those answers and bolt mod 2 on the end – Beta Decay – 2018-08-01T20:25:37.163
2@BetaDecay but that doesn't work in reverse: i.e. you cannot take all of these answers and remove the mod 2. Therefore, this challenge invites some new methods. – Amphibological – 2018-08-01T20:29:53.053
@BetaDecay side note: is there meta consensus as to what constitutes a duplicate? – Amphibological – 2018-08-01T20:35:47.113
A challenge is a dupe if a trivial change can be made to answers of the original challenge to make them suitable for the duplicate challenge. Backwards compatibility is AFAIK not counted – Beta Decay – 2018-08-01T20:44:22.640
@BetaDecay is there anywhere on meta that says that or not? Also, if this really is a dupe, what should I do? I still think this challenge can invite some different approaches. – Amphibological – 2018-08-01T20:46:18.447
Quick question: does the output need to be true/false or can it be 1/0? – Robert S. – 2018-08-01T20:54:37.187
@RobertS. any two values that are considered truthy and falsy in your language are fine. – Amphibological – 2018-08-01T20:55:47.410
Related: How on earth did llhuii output the Evil Numbers in 42 bytes of Python?
– nwellnhof – 2018-08-01T22:53:12.96014I believe that
666 => False
should be a test case. – user2390246 – 2018-08-02T10:38:27.870@JoKing if by broken you mean the random HTML at the bottom, that's due to misformatted answers. I've tried to edit or comment on all of those. – Amphibological – 2018-08-04T15:45:28.177
Why would you call this an "evil" number. There is a long standing tradition to declare the binary representation of the number as being "even parity". – Michael Karas – 2018-08-04T18:23:27.783
@user2390246 finally, done. ;) – Amphibological – 2018-08-04T22:17:45.240
Leaderboard says
"message": "Uncaught TypeError: Cannot read property 'forEach' of undefined", "filename": "https://stacksnippets.net/js", "lineno": 122, "colno": 18"
– Simon Forsberg – 2018-08-05T13:56:28.157@SimonForsberg sorry, that doesn't happen for me...i don't know why it's happening for you. – Amphibological – 2018-08-05T14:30:09.890
1
@Amphibological I found out why, I'm getting "too many requests from this IP, more requests available in 57696 seconds" back from the Stack Exchange API. I think I know who to blame though.
– Simon Forsberg – 2018-08-05T17:04:34.0831Yes, 666 is not evil, but 616 is. More evidence corroborating Papyrus 115! – aschepler – 2018-08-07T11:41:20.043