"He had to discover it by himself."
"He had to discover it for himself."
The usage of "by himself" and "for himself" are very similar.
However, when you do something "by yourself", it generally means you do it "alone" without any help.
On the other hand, when you do something "for yourself", it means that you want to do it "for your own sake", in a way so that you accomplish something or get some additional benefit out of doing it.
He went to the library so that he could study by himself. (Alone, without any distractions, maybe so he could get his homework done faster.)
He went to the library to study for himself. (Maybe he especially wanted to do some research in some field he is very interested in.)
"By" and "for" in this usage have really subtle differences. I hope this helps.