The Mathematics of Magic