Getting the answer is much less important than working on the problem.

There’s a reason most upper level math books don’t provide answer keys for the odd problems in the back of the book- like how calculus books do.

The learning is in the attempt, actually getting the solution is not the priority.

It’s great that you are spending 3-4 hours on a problem, trying different approaches!!! That’s a taste ( a very small one, but we must start somewhere) of what mathematics research is like.

If you really get stuck, it’s far better to ask for advice on how to solve the problem, instead of the answer. Ask if you are along the right path, or for advice on where to start the next step.

Both, going online or asking your professor for help are great ways of approaching a problem you are stuck on. But, don’t ask for the answer. - ask for what you are doing wrong or where you might start.

Also, it’s important to “play” with solutions. I think Terry Tao says it best:

“ When you learn mathematics, whether in books or in lectures, you generally only see the end product – very polished, clever and elegant presentations of a mathematical topic.

However, the process of discovering new mathematics is much messier, full of the pursuit of directions which were naïve, fruitless or uninteresting.

While it is tempting to just ignore all these “failed” lines of inquiry, actually they turn out to be essential to one’s deeper understanding of a topic, and (via the process of elimination) finally zeroing in on the correct way to proceed.

So one should be unafraid to ask “stupid” questions, challenging conventional wisdom on a subject; the answers to these questions will occasionally lead to a surprising conclusion, but more often will simply tell you why the conventional wisdom is there in the first place, which is well worth knowing.

For instance, given a standard lemma in a subject, you can ask what happens if you delete a hypothesis, or attempt to strengthen the conclusion; if a simple result is usually proven by method X, you can ask whether it can be proven by method Y instead; the new proof may be less elegant than the original, or may not work at all, but in either case it tends to illuminate the relative power of methods X and Y, which can be useful when the time comes to prove less standard lemmas.”