Skip to content

Instantly share code, notes, and snippets.

@steren
Created January 26, 2013 00:03
Show Gist options
  • Save steren/4639022 to your computer and use it in GitHub Desktop.
Save steren/4639022 to your computer and use it in GitHub Desktop.
Josephus problem illustration and formula test
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="http://code.shutterstock.com/rickshaw/src/css/graph.css">
<script src="http://code.shutterstock.com/rickshaw/vendor/d3.v2.js"></script>
<script src="http://code.shutterstock.com/rickshaw/rickshaw.js"></script>
<meta charset=utf-8 />
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
</style>
<title>KILL</title>
</head>
<body>
<div id="chart"></div>
<ul id="results"></ul>
</body>
<script>
var result = [];
var killThem = function(n) {
var guys = new Array(n);
var numberKill = 0;
var i = 0;
while(numberKill < n) {
numberKill++;
if(numberKill === n) {break;}
guys[i] = true;
while(guys[i]) {
i = (i+1) % n;
}
i = (i+1) % n;
while(guys[i]) {
i = (i+1) % n;
}
}
i++;
result.push({x:n, y:i});
var position = 2 * ( n - Math.pow(2, Math.floor( Math.log(n)/Math.log(2) )));
$("#results").append("<li>" + n + " : " + i + " - " + position + "</li>");
};
for(var j = 1; j < 1000; j++) {
killThem(j);
}
// ploting
var graph = new Rickshaw.Graph( {
element: document.getElementById("chart"),
width: 960,
height: 500,
renderer: 'scatterplot',
series: [
{
color: "#ff9030",
data: result,
}
]
} );
graph.renderer.dotSize = 3;
graph.render();
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment