You are viewing a single comment's thread. Return to all comments →
// c#
List<int> linear_s = []; List<List<int>> ideal_s = [ [2, 7, 6, 9, 5, 1, 4, 3, 8] , [6, 7, 2, 1, 5, 9, 8, 3, 4] , [4, 3, 8, 9, 5, 1, 2, 7, 6] , [8, 3, 4, 1, 5, 9, 6, 7, 2] , [2, 9, 4, 7, 5, 3, 6, 1, 8] , [4, 9, 2, 3, 5, 7, 8, 1, 6] , [6, 1, 8, 7, 5, 3, 2, 9, 4] , [8, 1, 6, 3, 5, 7, 4, 9, 2] ]; int calc_cost = 0, cost = 99; foreach(var s1 in s) { foreach(int s2 in s1) { linear_s.Add(s2); } } foreach (List<int> si in ideal_s) { calc_cost = 0; for (int i=0; i<si.Count; i++) { calc_cost += Math.Abs(si[i] - linear_s[i]); } if (calc_cost < cost) { cost = calc_cost; } } return cost; }
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 →
// c#