You are viewing a single comment's thread. Return to all comments →
function formingMagicSquare(s) { var M = 15 var y = 5 var x = 3 var j = [... new Array(3)].map(x =>Array(3).fill()) var flat_s = s.flat() const magicSquares = [ [8, 1, 6, 3, 5, 7, 4, 9, 2], [6, 1, 8, 7, 5, 3, 2, 9, 4], [4, 9, 2, 3, 5, 7, 8, 1, 6], [2, 9, 4, 7, 5, 3, 6, 1, 8], [8, 3, 4, 1, 5, 9, 6, 7, 2], [4, 3, 8, 9, 5, 1, 2, 7, 6], [6, 7, 2, 1, 5, 9, 8, 3, 4], [2, 7, 6, 9, 5, 1, 4, 3, 8] ]; var flat_squres = magicSquares.flat() var sum = Infinity var current_sum = 0 var j = 0 for(var i = 0; i < flat_squres.length;i++){ console.log(`m ${flat_squres[i]} vs ${flat_s[j]}`) current_sum += Math.abs(flat_squres[i] - flat_s[j]) j = j + 1 if (j == 9){ sum = sum > current_sum ? current_sum : sum current_sum = 0 j = 0 } } console.log(sum) return sum }
Seems like cookies are disabled on this browser, please enable them to open this website
Forming a Magic Square
You are viewing a single comment's thread. Return to all comments →