The partition function of Z(2) and Z(8) lattice gauge theory in four dimensions, a novel approach to simulations of lattice systems