A new method for the partition function of discrete systems with application to the 3D Ising model