I will attempt an explanation of this formula. This is an overview. If you need more detail you should give more information about the problem you are trying to solve.
First let's talk about gambling. The first rule of gambling is that if there is a negative expectation, you should not bet at all. If you have a positive expectation and you have finite capital, then it is generally a good idea to make several bets, not just one. This is because of the Risk of Ruin issue: you could be wiped out due to bad luck on the first one or a few consecutive trades. There is a mathematical theory of RoR, but to keep it simple you could just adopt a rule that you want to survive 20 losing trades, so you would bet 5% of your initial capital on each bet. This is just a rule of thumb, if you are daring you might bet 10% and if you are cautious you might bet 3% (as you rule does). Nothing very scientific here, it is a subjective preference.
Now let's talk about trading financial instruments. The main difference is gambling has a binary (win/loss) outcome while trading has a continuous outcome. So risk measurement is more difficult. We can't use the worst loss because the worst that can happen to a stock is it goes to 0 and that's a 100% loss, though its rare. A rule based on the worst outcome would say we should not bet at all. To get around this we might say: I want to survive 20 "really bad trades" (RBTs) where a RBT is a big loss but not a complete wipeout. For example Value at Risk is defined as a loss that is exceeded fewer than 5% of the time. (Again a "magic 5%" that someone pulled out of the air, though it seems plausible and is commonly used). For intraday trading a plausible RBT would be a trade where you buy at the high of the day and sell at the low of the day. The range (=high-low) is a random variable but we could estimate it using the ATR (average true range, over say the last 60 days). Since the range (and risk in general) fluctuates over time and the ATR is an average measured in a past period, we could use really_bad_loss = 2*ATR to be on the safe side for the future.
The result of all this "reasoning" is the formula you gave for the number of shares to hold:
N = (0.03 capital) / (price_per_share / (2*ATR))
capital is in USD, price_per share is in US/shares and ATR is a pure number, so dimensionally the result is shares, as we wanted.
Note the following limitations:
It assumes day trading: once you get in you have to get the $\bullet$ out (GTFO in polite language) the same day. That limits your flexibility. For multi-day trading you would need more capital (you need to survive a price excursion of more that 2 ATR).
It assumes you will use leverage, and computes for you how much leverage you will use. If the leverage ratio is > 1 (as in your numerical example) you will be buying on margin, which is not always a good idea (it increases the risk of ruin). If you don't use margin you cannot use this rule.
It is an example of a "risk only" position sizing rule, which looks only at the down side and does not take into account the (assumed positive) expectation. Because estimating the expectation is difficult, this is quite common in position sizing. VaR is another example. But there are also rules (such as the Kelly Rule mentioned in a comment above) that take the expectation in account. It is a static rule, based on the initial capital and not on the current capital as it evolves over time (another feature of the Kelly rule).
The coefficients 0.03 in the numerator and 2 in the denominator are somewhat arbitrary, as discussed.
Hope this helps. Seeing the reasoning behind the rule could be helpful, since it also shows the limitations. No one should rely on a mathematical formula without understanding the justification behind it.