# How many Magic Cubes?

You all know what a magic square is! But what about a magic cube? A magic cube is the 3D version of a magic square, and is a set of numbers in a cube, where the sums of every straight (not diagonal) line are equal. You must write a program or function that receives an integer, and returns the number of valid magic cubes made up of up to one of each integer between 1 and that integer (inclusive).

An integer.

## Output:

The number of magic cubes you can make by using up to one of of each integer between 1 and the input. A cube with various rotations should only be counted once.

## Scoring:

This is , lowest byte count wins.

## BOUNTY:

I will award a 100 rep bounty to the first user that manages to do this in < 100 bytes without using built-ins designed specifically towards magic cubes or squares (looking at you Mathematica).

• So if a bunch of users all get <100 bytes you'll give +100 to all of them? Jun 14, 2017 at 1:08
• "Between integer 1 and the input." Inclusive or exclusive? Jun 14, 2017 at 1:08
• Test cases would be good.
– xnor
Jun 14, 2017 at 1:12
• The given magic cube is definitely not valid, since 9+31+60=100≠106=34+43+29. Jun 14, 2017 at 8:13
• You should add some test cases Jun 14, 2017 at 8:51

# Clingo, 220 + 2 = 222 bytes

1{s(S):S=0..n,S**3==n}.
A{a(0..n-1,A)}:-A=1..n.
:-a(P,A-1),{a(P,A)}0,A<n.
:-s(S),{a(P,A):P-P/K\S*K=Q-Q/K\S*K}>(S*n+S)/2,Q=0..n,K=(1;S;S*S).
:-s(S),a(((0;1)+(0;S)+(0;S*S))*~-S,A),{a(0,A)}0.
:-s(S),a((S;S*S)*~-S,A),{a(S-1,A)}0.


Run with clingo magic-cubes.lp -n0 -cn=INPUT. The answer will be printed next to Models.

All newlines are optional and shown here for clarity but excluded from the byte count. I’m scoring +2 bytes for -n0 (“count all models”).

The last two lines ensure that only one rotated version of each cube is counted.

The answers for 1, 8, 27 are quickly found to be 1, 0, 216; the rest are probably too hard.

If you want to see the cubes in a more readable format, add b((P\S,P/S\S,P/S/S),A):s(S),-a(P,A),not a(P,A-1). #show b/2.

$clingo magic-cubes.lp -n0 -cn=1 clingo version 5.1.0 Reading from magic-cubes.lp Solving... Answer: 1 a(0,1) s(1) SATISFIABLE Models : 1 Calls : 1 Time : 0.009s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s) CPU Time : 0.010s$ clingo magic-cubes.lp -n0 -cn=8
clingo version 5.1.0
Solving...
UNSATISFIABLE

Models       : 0
Calls        : 1
Time         : 0.010s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time     : 0.000s
\$ clingo magic-cubes.lp -n0 -cn=27
clingo version 5.1.0
Solving...
a(7,1) a(7,2) a(13,2) a(7,3) a(13,3) a(19,3) a(7,4) a(13,4) a(15,4) a(19,4) a(7,5) a(13,5) a(15,5) a(19,5) a(21,5) a(0,6) a(7,6) a(13,6) a(15,6) a(19,6) a(21,6) a(0,7) a(7,7) a(13,7) a(15,7) a(19,7) a(21,7) a(26,7) a(0,8) a(5,8) a(7,8) a(13,8) a(15,8) a(19,8) a(21,8) a(26,8) a(0,9) a(5,9) a(7,9) a(11,9) a(13,9) a(15,9) a(19,9) a(21,9) a(26,9) a(0,10) a(2,10) a(5,10) a(7,10) a(11,10) a(13,10) a(15,10) a(19,10) a(21,10) a(26,10) a(0,11) a(2,11) a(5,11) a(7,11) a(11,11) a(13,11) a(15,11) a(17,11) a(19,11) a(21,11) a(26,11) a(0,12) a(2,12) a(5,12) a(7,12) a(11,12) a(13,12) a(15,12) a(17,12) a(19,12) a(21,12) a(23,12) a(26,12) a(0,13) a(2,13) a(5,13) a(7,13) a(10,13) a(11,13) a(13,13) a(15,13) a(17,13) a(19,13) a(21,13) a(23,13) a(26,13) a(0,14) a(2,14) a(5,14) a(7,14) a(10,14) a(11,14) a(13,14) a(15,14) a(17,14) a(19,14) a(21,14) a(23,14) a(25,14) a(26,14) a(0,15) a(2,15) a(4,15) a(5,15) a(7,15) a(10,15) a(11,15) a(13,15) a(15,15) a(17,15) a(19,15) a(21,15) a(23,15) a(25,15) a(26,15) a(0,16) a(2,16) a(4,16) a(5,16) a(7,16) a(10,16) a(11,16) a(13,16) a(15,16) a(17,16) a(18,16) a(19,16) a(21,16) a(23,16) a(25,16) a(26,16) a(0,17) a(2,17) a(4,17) a(5,17) a(6,17) a(7,17) a(10,17) a(11,17) a(13,17) a(15,17) a(17,17) a(18,17) a(19,17) a(21,17) a(23,17) a(25,17) a(26,17) a(0,18) a(2,18) a(4,18) a(5,18) a(6,18) a(7,18) a(10,18) a(11,18) a(12,18) a(13,18) a(15,18) a(17,18) a(18,18) a(19,18) a(21,18) a(23,18) a(25,18) a(26,18) a(0,19) a(2,19) a(3,19) a(4,19) a(5,19) a(6,19) a(7,19) a(10,19) a(11,19) a(12,19) a(13,19) a(15,19) a(17,19) a(18,19) a(19,19) a(21,19) a(23,19) a(25,19) a(26,19) a(0,20) a(2,20) a(3,20) a(4,20) a(5,20) a(6,20) a(7,20) a(9,20) a(10,20) a(11,20) a(12,20) a(13,20) a(15,20) a(17,20) a(18,20) a(19,20) a(21,20) a(23,20) a(25,20) a(26,20) a(0,21) a(2,21) a(3,21) a(4,21) a(5,21) a(6,21) a(7,21) a(9,21) a(10,21) a(11,21) a(12,21) a(13,21) a(15,21) a(17,21) a(18,21) a(19,21) a(21,21) a(23,21) a(24,21) a(25,21) a(26,21) a(0,22) a(2,22) a(3,22) a(4,22) a(5,22) a(6,22) a(7,22) a(9,22) a(10,22) a(11,22) a(12,22) a(13,22) a(14,22) a(15,22) a(17,22) a(18,22) a(19,22) a(21,22) a(23,22) a(24,22) a(25,22) a(26,22) a(0,23) a(2,23) a(3,23) a(4,23) a(5,23) a(6,23) a(7,23) a(9,23) a(10,23) a(11,23) a(12,23) a(13,23) a(14,23) a(15,23) a(17,23) a(18,23) a(19,23) a(20,23) a(21,23) a(23,23) a(24,23) a(25,23) a(26,23) a(0,24) a(2,24) a(3,24) a(4,24) a(5,24) a(6,24) a(7,24) a(8,24) a(9,24) a(10,24) a(11,24) a(12,24) a(13,24) a(14,24) a(15,24) a(17,24) a(18,24) a(19,24) a(20,24) a(21,24) a(23,24) a(24,24) a(25,24) a(26,24) a(0,25) a(2,25) a(3,25) a(4,25) a(5,25) a(6,25) a(7,25) a(8,25) a(9,25) a(10,25) a(11,25) a(12,25) a(13,25) a(14,25) a(15,25) a(17,25) a(18,25) a(19,25) a(20,25) a(21,25) a(22,25) a(23,25) a(24,25) a(25,25) a(26,25) a(0,26) a(1,26) a(2,26) a(3,26) a(4,26) a(5,26) a(6,26) a(7,26) a(8,26) a(9,26) a(10,26) a(11,26) a(12,26) a(13,26) a(14,26) a(15,26) a(17,26) a(18,26) a(19,26) a(20,26) a(21,26) a(22,26) a(23,26) a(24,26) a(25,26) a(26,26) a(0,27) a(1,27) a(2,27) s(3) a(3,27) a(4,27) a(5,27) a(6,27) a(7,27) a(8,27) a(9,27) a(10,27) a(11,27) a(12,27) a(13,27) a(14,27) a(15,27) a(16,27) a(17,27) a(18,27) a(19,27) a(20,27) a(21,27) a(22,27) a(23,27) a(24,27) a(25,27) a(26,27)
[…]
a(0,2) a(12,1) a(12,2) a(0,3) a(12,3) a(24,3) a(0,4) a(12,4) a(22,4) a(24,4) a(0,5) a(10,5) a(12,5) a(22,5) a(24,5) a(0,6) a(7,6) a(10,6) a(12,6) a(22,6) a(24,6) a(0,7) a(5,7) a(7,7) a(10,7) a(12,7) a(22,7) a(24,7) a(0,8) a(5,8) a(7,8) a(10,8) a(12,8) a(20,8) a(22,8) a(24,8) a(0,9) a(5,9) a(7,9) a(10,9) a(12,9) a(17,9) a(20,9) a(22,9) a(24,9) a(0,10) a(5,10) a(7,10) a(10,10) a(12,10) a(16,10) a(17,10) a(20,10) a(22,10) a(24,10) a(0,11) a(4,11) a(5,11) a(7,11) a(10,11) a(12,11) a(16,11) a(17,11) a(20,11) a(22,11) a(24,11) a(0,12) a(4,12) a(5,12) a(7,12) a(10,12) a(12,12) a(16,12) a(17,12) a(19,12) a(20,12) a(22,12) a(24,12) a(0,13) a(4,13) a(5,13) a(7,13) a(10,13) a(12,13) a(16,13) a(17,13) a(19,13) a(20,13) a(22,13) a(24,13) a(26,13) a(0,14) a(4,14) a(5,14) a(7,14) a(10,14) a(12,14) a(14,14) a(16,14) a(17,14) a(19,14) a(20,14) a(22,14) a(24,14) a(26,14) a(0,15) a(2,15) a(4,15) a(5,15) a(7,15) a(10,15) a(12,15) a(14,15) a(16,15) a(17,15) a(19,15) a(20,15) a(22,15) a(24,15) a(26,15) a(0,16) a(2,16) a(4,16) a(5,16) a(6,16) a(7,16) a(10,16) a(12,16) a(14,16) a(16,16) a(17,16) a(19,16) a(20,16) a(22,16) a(24,16) a(26,16) a(0,17) a(2,17) a(4,17) a(5,17) a(6,17) a(7,17) a(10,17) a(12,17) a(14,17) a(16,17) a(17,17) a(19,17) a(20,17) a(21,17) a(22,17) a(24,17) a(26,17) a(0,18) a(2,18) a(4,18) a(5,18) a(6,18) a(7,18) a(9,18) a(10,18) a(12,18) a(14,18) a(16,18) a(17,18) a(19,18) a(20,18) a(21,18) a(22,18) a(24,18) a(26,18) a(0,19) a(2,19) a(4,19) a(5,19) a(6,19) a(7,19) a(9,19) a(10,19) a(11,19) a(12,19) a(14,19) a(16,19) a(17,19) a(19,19) a(20,19) a(21,19) a(22,19) a(24,19) a(26,19) a(0,20) a(2,20) a(4,20) a(5,20) a(6,20) a(7,20) a(8,20) a(9,20) a(10,20) a(11,20) a(12,20) a(14,20) a(16,20) a(17,20) a(19,20) a(20,20) a(21,20) a(22,20) a(24,20) a(26,20) a(0,21) a(2,21) a(4,21) a(5,21) a(6,21) a(7,21) a(8,21) a(9,21) a(10,21) a(11,21) a(12,21) a(14,21) a(16,21) a(17,21) a(19,21) a(20,21) a(21,21) a(22,21) a(23,21) a(24,21) a(26,21) a(0,22) a(2,22) a(4,22) a(5,22) a(6,22) a(7,22) a(8,22) a(9,22) a(10,22) a(11,22) a(12,22) a(14,22) a(16,22) a(17,22) a(18,22) a(19,22) a(20,22) a(21,22) a(22,22) a(23,22) a(24,22) a(26,22) a(0,23) a(2,23) a(4,23) a(5,23) a(6,23) a(7,23) a(8,23) a(9,23) a(10,23) a(11,23) a(12,23) a(14,23) a(15,23) a(16,23) a(17,23) a(18,23) a(19,23) a(20,23) a(21,23) a(22,23) a(23,23) a(24,23) a(26,23) a(0,24) a(2,24) a(3,24) a(4,24) a(5,24) a(6,24) a(7,24) a(8,24) a(9,24) a(10,24) a(11,24) a(12,24) a(14,24) a(15,24) a(16,24) a(17,24) a(18,24) a(19,24) a(20,24) a(21,24) a(22,24) a(23,24) a(24,24) a(26,24) a(0,25) a(1,25) a(2,25) a(3,25) a(4,25) a(5,25) a(6,25) a(7,25) a(8,25) a(9,25) a(10,25) a(11,25) a(12,25) a(14,25) a(15,25) a(16,25) a(17,25) a(18,25) a(19,25) a(20,25) a(21,25) a(22,25) a(23,25) a(24,25) a(26,25) a(0,26) a(1,26) a(2,26) a(3,26) a(4,26) a(5,26) a(6,26) a(7,26) a(8,26) a(9,26) a(10,26) a(11,26) a(12,26) a(14,26) a(15,26) a(16,26) a(17,26) a(18,26) a(19,26) a(20,26) a(21,26) a(22,26) a(23,26) a(24,26) a(25,26) a(26,26) a(0,27) a(1,27) a(2,27) s(3) a(3,27) a(4,27) a(5,27) a(6,27) a(7,27) a(8,27) a(9,27) a(10,27) a(11,27) a(12,27) a(13,27) a(14,27) a(15,27) a(16,27) a(17,27) a(18,27) a(19,27) a(20,27) a(21,27) a(22,27) a(23,27) a(24,27) a(25,27) a(26,27)
SATISFIABLE

Models       : 216
Calls        : 1
Time         : 6.101s (Solving: 6.07s 1st Model: 1.03s Unsat: 0.37s)
CPU Time     : 6.090s

• I don't think that's what the question is asking for Jun 18, 2017 at 9:38
• This appears to calculate the number of valid magic cubes with side length n, whereas the question is asking for the number for valid magic cubes in which every number is unique and less than or equal to n. Jun 18, 2017 at 11:04
• Yes, this is incorrect. Jul 22, 2017 at 11:50
• @ASCII-only Fixed. Jul 22, 2017 at 16:32

# Ruby, 275 bytes

n=gets.to_i;p (2.upto(n**(1/3r)).map{|i|(1..n).to_a.permutation(i**3).map{|x|c=eval'x'+".each_slice(#{i}).to_a"*2;[c,c.map(&:transpose),c.transpose.map(&:transpose)].map{|q|q.map{|a|a.map{|b|b.inject(:+)}}.flatten.uniq.length}.inject(:+)==3?1:0}}+[0]).flatten.inject(:+)/48+n


Try it online!

In theory, this works. In practice, since it creates every permutation of the list of possible numbers, it'll take hopelessly large amounts of time and memory for any input more than 13 or so. I haven't taken the time to test it past 12, but it seems no 2×2×2 magic cubes exist for that range, so all it really does is tell you how many 1×1×1 cubes you can make.

I "golfed" it a bit (really just made it hard to read for minimal size reduction returns). I'm sure the size can be reduced quite a bit, though getting it under 100 characters is a pretty tall order.

Ruby 2.4 added .sum, but TIO doesn't support it yet, so this has a few .inject(:+)s.

I'll add a non-golfed version + explanation if requested.

• There are no 2×2×2 magic cubes with any range. The top face would have to be a 2×2 magic square, but there aren't any of those either: a1+a2 = a1+b1, so a2=b1, which violates the rule that all the values must be distinct.
– user62131
Jun 14, 2017 at 21:54
• @ais523 You're right; I hadn't thought of that. In that case, I'm going to abandon this solution seeing as it's not a viable way of finding 3-cubes. Jun 15, 2017 at 0:16

# Jelly, 36 bytes

;Z;Z€ẎS€E
*3³œcŒ!€Ẏs€s€¹Ç€S
Ç€ḊS÷24+


Try it online!

As you expect, it's terribly slow.

## Explanation

• Link 1: Given a cube, check if it's valid.
;Z;Z€ẎS€E   Input: N =
[  [ [a,b],    [ [e,f],
[c,d] ],    [g,h] ]   ]

;Z          Concatenate with zip of N. The zip (transpose) of N is
[   [ [a,b],    [ [c,d],
[e,f] ],    [g,h] ]   ]
(by treating each 2-element block as an unit
and transpose the large table)

;Z€         Concatenate with plane-wise zip of N, that is
[  [ [a,c],    [ [e,g],
[b,d] ],    [f,h] ]   ]
(transpose each 2x2 block)

The current value is:
[ [ [a,b],   [ [e,f],   [ [a,b],   [ [c,d],   [ [a,c],   [ [e,g],
[c,d] ],   [g,h] ],   [e,f] ],   [g,h] ],   [b,d] ],   [f,h] ] ]

Ẏ           Tighten, concatenate all elements inside, similar to ;/.
Get [ [a,b], [c,d], [e,f], [g,h],
[a,b], [e,f], [c,d], [g,h],
[a,c], [b,d], [e,g], [f,h] ]
S€          Sum each sublist. ZS also works.
E           All Equal? 1 if all elements are equal, 0 otherwise.

• Link 2: Given a side length x, generate all cubes with that side length and calculate number of valid cubes.
*3³œcŒ!€Ẏs€s€¹Ç€S   Given: x (side length).
*3                  Calculate x³.
³                 First commandline argument. (n)
œc               Generate all subsets of the range [1..n] having length x³.
Œ!€Ẏ           All permutation of each and tighten.
(the current value is a list of all ways to choose
an ordered tuple of x³ elements from [1..n])
s€s€       Split each twice into slices of length x.
On a list of (list of x³ integers), this format
each sublist into a cube.
¹      Do nothing. Because Jelly is a tacit programming language
without this the program will behave differently.
Ç€    Apply last link for €ach.
S   Sum.

• Link 3 (main link): For all side lengths from 1 to n, calculate number of cubes (rotations are counted multiple times) for each side length, dequeue and sum it (so we get the sum of number of cubes with side length >= 2), divide by 24 (to avoid duplication of cubes) and add the number of side length 1 cube (input).

Ç          Apply last link...
€           for €ach element from 1 to n.
Ḋ        Ḋequeue, dispose the result for value 1.
S÷24    Sum and divide by 24.
+   Add the input (number of cubes with side length 1)