Magic squares of doubly even order: Difference between revisions
Content added Content deleted
(Added Go implementation) |
(Corrected my existing code) |
||
Line 898: | Line 898: | ||
) |
) |
||
const |
const dimensions int = 8 |
||
func |
func setupMagicSquareData(d int) ([][]int, error) { |
||
var output [][]int |
var output [][]int |
||
⚫ | |||
lastRowLen := len(array) % width |
|||
⚫ | |||
for i := 0; i < len(array)-lastRowLen; i += width { |
|||
var row []int |
|||
⚫ | |||
row = append(row, array[i+j]) |
|||
⚫ | |||
⚫ | |||
} |
|||
if lastRowLen != 0 { |
|||
⚫ | |||
} |
|||
return output |
|||
} |
|||
func setupMagicSquareData(width int) ([]int, error) { |
|||
var output []int |
|||
⚫ | |||
⚫ | |||
} |
} |
||
var bits uint = 0x9669 // 0b1001011001101001 |
|||
cornerWidth := width / 4 |
|||
size := d * d |
|||
mult := d / 4 |
|||
⚫ | |||
if val := i + int(1); val%width <= cornerWidth || val%width > width-cornerWidth { |
|||
output = append(output, []int{}) |
|||
for c := 0; c < d; i, c = i+1, c+1 { |
|||
⚫ | |||
bitPos := c/mult + (r/mult)*4 |
|||
output = append(output, arrayLen-i) |
|||
if (bits & (1 << uint(bitPos))) != 0 { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
} |
} |
||
Line 942: | Line 931: | ||
func main() { |
func main() { |
||
data, err := setupMagicSquareData( |
data, err := setupMagicSquareData(dimensions) |
||
if err != nil { |
if err != nil { |
||
log.Fatal(err) |
log.Fatal(err) |
||
} |
} |
||
⚫ | |||
rows := splitArray(data, squareWidth) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
fmt.Println(strings.Join(arrayItoa(row), " ")) |
fmt.Println(strings.Join(arrayItoa(row), " ")) |
||
} |
} |
||
fmt.Printf("\nMagic Constant: %d\n", magicConstant) |
fmt.Printf("\nMagic Constant: %d\n", magicConstant) |
||
} |
} |
||
</lang> |
</lang> |
||
Line 958: | Line 945: | ||
<pre> 1 2 62 61 60 59 7 8 |
<pre> 1 2 62 61 60 59 7 8 |
||
9 10 54 53 52 51 15 16 |
9 10 54 53 52 51 15 16 |
||
48 47 19 20 21 22 42 41 |
|||
40 39 27 28 29 30 34 33 |
|||
32 31 35 36 37 38 26 25 |
|||
24 23 43 44 45 46 18 17 |
|||
49 50 14 13 12 11 55 56 |
49 50 14 13 12 11 55 56 |
||
57 58 6 5 4 3 63 64 |
57 58 6 5 4 3 63 64 |
||
Magic Constant: 260 |
Magic Constant: 260 |
||
</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |