Skip to content

Instantly share code, notes, and snippets.

@openorclose
Forked from 140bytes/LICENSE.txt
Last active December 16, 2015 07:59
Show Gist options
  • Save openorclose/5402962 to your computer and use it in GitHub Desktop.
Save openorclose/5402962 to your computer and use it in GitHub Desktop.

140byt.es Magic Square Generator

Create a magic square of order s with this! (currently s%4==0 and odd s supported only)

function(s,q,r,n){for(n=s*s;q[r=0|--n/s][c=n%s]=s%4?(0|s/2+r+c+1)%s*s+(r+2*c+1)%s:r%4-c%4&&r%4+c%4-3?s*s-1-n:n,n;);}
function(s,q,r,n){for(n=s*s;q[r=0|--n/s][c=n%s]=s%4?(0|s/2+r+c+1)%s*s+(r+2*c+1)%s:r%4-c%4&&r%4+c%4-3?s*s-1-n:n,n;);}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "magicSquare",
"description": "Creates a magic square of order s. Currently works only for odd s and s%4=0",
"keywords": [
"magic",
"square"
]
}
<!DOCTYPE html>
<title>Magic Square</title>
<div>Expected value: <b>undefined</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(s,q,r,n){for(n=s*s;q[r=0|--n/s][c=n%s]=s%4?(0|s/2+r+c+1)%s*s+(r+2*c+1)%s:r%4-c%4&&r%4+c%4-3?s*s-1-n:n,n;);};
function make2dSquareArray (size) {
var array = [];
while (size--) {
array.push([]);
}
return array;
}
function htmlTableFrom2dArray(array) {
var table = ["<table>"];
for (var i = 0; i < array.length; i++) {
table.push ("<tr>");
for (var j = 0; j < array.length; j++) {
table.push ("<td>", array[i][j], "</td>");
}
table.push ("</tr>");
}
table.push ("</table>");
return table.join ("");
}
var square = make2dSquareArray (5);
generateOdd(5,square);
document.getElementById ("ret").innerHTML=htmlTableFrom2dArray (square);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment