Season 2, Episode 12, "The Royale." This was subsequently tangentially referenced in an episode of "Deep Space Nine," which means "explanations" about Wiles never being born in the timelines are wrong.
Quoting from Wikipedia:
In "The Royale", an episode (first aired 27 March 1989)
of Star Trek: The Next Generation, Captain Picard states
that the theorem had gone unsolved for 800 years. At the
end of the episode Captain Picard says,
"Like Fermat's theorem, it is a puzzle we
may never solve."
Wiles' proof was released five years after the particular
episode aired. This was subsequently mentioned in a Star
Trek: Deep Space Nine episode called "Facets" during June
1995 in which Jadzia Dax comments that one of her previous
hosts, Tobin Dax, had
"the most original approach to the proof
since Wiles over 300 years ago."
This reference was generally understood by fans to be a
retroactive continuity for "The Royale".