Skip to content

Instantly share code, notes, and snippets.

@NelsonMinar
Created July 29, 2012 21:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NelsonMinar/3201872 to your computer and use it in GitHub Desktop.
Save NelsonMinar/3201872 to your computer and use it in GitHub Desktop.
Cantor pairing function
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<title>Cantor pairing function</title>
<style>
td { min-width: 2em; text-align: right; }
</style>
</head>
<body>
<p>Mapping tuples (n, m) to the integers, see <a href="http://en.wikipedia.org/wiki/Cantor_pairing_function#Cantor_pairing_function">Cantor pairing function</a>.
<table id="cantor"></table>
<script>
function f(x, y) {
return ((x + y) * (x + y + 1)) / 2 + y;
}
for (var y = 0; y < 100; y++) {
var line = ['<tr>'];
for (var x = 0; x < 30; x++) {
line.push("<td>" + f(y, x) + "</td>");
}
line.push("</tr>")
$("#cantor").append(line.join(''));
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment