html
html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!DOCTYPE html>
<html>
<head>
<title>[Challenge] Odious numbers smaller than 10000</title>
</head>
<body>
<h1>Challenge: how many odious numbers smaller than 10.000 can you find [<a href='https://www.sololearn.com/Discuss/915458/' target='_blank'>link</a>]</h1>
<h2>Definition</h2>
<p>Odious numbers are non-negative numbers that have an <u>odd number of ones in the binary expansion</u>.<br>
# 21 = 10101 = 3<br>
# 50 = 110010 = 3</p>
<script>
// "Odious" numbers challenge: https://www.sololearn.com/Discuss/915458/
var n = 0,
current = 0;
function writeOut(count, number, binary, current) {
return "<tr><td>" + count + "</td><td>" + number + "</td><td>" + binary + "</td><td>" + current + "</td></tr>\n";
}
document.write("<table><tr><th>Count</th><th>Number</th><th>Binary</th><th>Ones</th></tr>\n");
for (var i=0; i<10000; i++) {
current = i.toString(2).replace(/0/g, '').length;
if (current % 2 !== 0) {
document.write(writeOut(n+=1, i, i.toString(2).replace(/1/g, '<b>1</b>'), current));
}
}
document.write("</table>");
document.write('<p>Total "odious" numbers smaller than 10000: ', n, '.</p>');
</script>
Enter to Rename, Shift+Enter to Preview
css
css
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
body {
font-family: sans-serif;
}
h1 {
font-size: 22px;
}
h2 {
font-size: 14px;
}
table, th, td {
border: 1px solid #444;
}
th, td {
padding: 3px;
}
Enter to Rename, Shift+Enter to Preview
js
js
1
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run