In the Futurama episode The Prisoner Of Benda (2010), the following plot line exists:
Professor Farnsworth and Amy build a machine that allows them to switch minds so that they may each pursue their lifelong dreams. However, they learn that the machine cannot be used twice on the same pairing of bodies. To try to return to their rightful bodies, they involve the rest of the crew in the mind switches, leaving each member free to pursue their own personal endeavors in a different crew member's body.
The episode was based on a Theorem proposed by series writer Ken Keeler. The Theorem attempts to solve the issue of how each crew member can be restored to their correct body given the limitation of the switching device.
It is established fact that the Futurama theorem/proof was created specifically for the franchise, for use in thethis episode "The Prisoner of Benda."
(Relevant parts of the interview, emphasis my own)
"[...] a theorem based on group theory was specifically written (and proven!) by staffer/PhD mathematician Ken Keeler to explain a plot twist."
"[...] a mathematical theorem was penned for the sake of entertainment."
Despite the specific wording above, it still seems to me open to debate as to whether or not the theorem was created for the sake of the plot of that episode, or if the plot was created for the sake of the theorem.
The man himself, Ken Keeler, was not the person interviewed, it was David X. Cohen, the show’s Executive Producer and head writer. Additionally, this was not a direct quote from him, but was the (possibly cherry picked) wording of the interviewer, Alaina G. Levine.
So...
Was the plot merely wrapped around the more-or-less already existing theorem?
...Alternatively...
Did the episode have the underlying problem, and then the theorem was created to fill that narrative hole?
Is this even publicly known?