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X will be called 2 times and ONE-X - 1 times?
perhaps should be
So for example, when X=8, THREE-X will be called 2 times and ONE-X - 3 times and TWO-X - 3 times?

I need somehow to break down the incoming number into 3s 2s and 1s, and giving them priority....
priority over what exactly ?