The algorithm is very simple. The element of the NxNxNx... hyper magic cube of order N in hyper space is given by

denote the residue of x under modulus N.