Also, the mathematician Ramanujan often stated that a god handed the solution to him (I remember something about doing mathematics in his dreams as well, but I'm not sure if I'm correct on this one).
This seems very interesting. Is there a way one can get so familiar with material that you can use it without effort? The key seems to be a good mind and a lot of work. Ramanujan had to be fed by his wife, because he would prefer doing math to eating dinner, and I imagine Donald Knuth is very intelligent and a hard worker too.
A thing I've noticed is that repeated exposure works better than long exposure. I sometimes come up with some neat ideas when I read stuff (usually mathematical or coding stuff).
It happened a couple times that I remembered such an idea, looked at it again, and discovered the idea would not work because of a single inconsistency. I am quite chaotic, and I imagine that smarter people would have this problem a lot less, but still I think this is a general principle that holds: working conciously on a problem causes tunnel vision. If you don't know how to solve an exercise in an hour, you're not very likely to solve it in ten if you just keep working on it.
If you let it rest and look at it again after a day or two, there is a moment where you try to recap and remember everything. In this instance, simple inconsistencies are easily spotted. Letting the problem rest gives you a chance to look at the problem with a fresh view, which is very important sometimes.