Magic squares of doubly even order: Difference between revisions

Corrected my existing code
(Added Go implementation)
(Corrected my existing code)
Line 898:
)
 
const squareWidthdimensions int = 8
 
func splitArraysetupMagicSquareData(array []int, widthd int) ([][]int, error) {
var output [][]int
if widthd < 4 || widthd%4 != 0 {
lastRowLen := len(array) % width
return [][]int{}, fmt.Errorf("Square widthdimension must be a positive number which is divisible by 4")
for i := 0; i < len(array)-lastRowLen; i += width {
var row []int
for j := 0; j < width; j++ {
row = append(row, array[i+j])
}
output = append(output, row)
}
if lastRowLen != 0 {
output = append(output, array[len(array)-lastRowLen:])
}
return output
}
 
func setupMagicSquareData(width int) ([]int, error) {
var output []int
if width < 4 || width%4 != 0 {
return []int{}, fmt.Errorf("Square width must be a positive number which is divisible by 4")
}
var bits uint = 0x9669 // 0b1001011001101001
cornerWidth := width / 4
arrayLensize := widthd * widthd
for imult := 0; i < arrayLen;d i++/ {4
for ji, r := 0, 0; jr < widthd; jr++ {
if val := i + int(1); val%width <= cornerWidth || val%width > width-cornerWidth {
output = append(output, val[]int{})
for c := 0; c < d; i, c = i+1, c+1 {
} else {
bitPos := c/mult + (r/mult)*4
output = append(output, arrayLen-i)
if (bits & (1 << uint(bitPos))) != 0 {
output[r] = append(output[r], rowi+1)
} else {
output[r] = append(output[r], array[len(array)size-lastRowLen:]i)
}
}
}
Line 942 ⟶ 931:
 
func main() {
data, err := setupMagicSquareData(squareWidthdimensions)
if err != nil {
log.Fatal(err)
}
magicConstant := (squareWidthdimensions * (squareWidthdimensions*squareWidthdimensions + 1)) / 2
rows := splitArray(data, squareWidth)
for _, row := range rowsdata {
magicConstant := (squareWidth * (squareWidth*squareWidth + 1)) / 2
for _, row := range rows {
fmt.Println(strings.Join(arrayItoa(row), " "))
}
fmt.Printf("\nMagic Constant: %d\n", magicConstant)
 
}
</lang>
Line 958 ⟶ 945:
<pre> 1 2 62 61 60 59 7 8
9 10 54 53 52 51 15 16
1748 1847 4619 4520 4421 4322 2342 2441
2540 2639 3827 3728 3629 3530 3134 3233
3332 3431 3035 2936 2837 2738 3926 4025
4124 4223 2243 2144 2045 1946 4718 4817
49 50 14 13 12 11 55 56
57 58 6 5 4 3 63 64
 
Magic Constant: 260</pre>
</pre>
 
=={{header|Haskell}}==