It doesn't even have to be a complex mathematical equation. Gerrymandering is a particularly acute American problem which isn't present in most other industrial democracies, because in most cases, an independent commission is picked to define district boundaries, and they are mostly guided by common sense and fairness, which seems to work well enough. The problem the U.S. has is to leave this task to people who have an inherent conflict of interest in the outcome of the process.