LLMs are not the ideal tool for this job, because LLMs cannot do math or count.
But LLM can write code that can do math and count. Tool use, more broadly, has proven to be a very powerful way to let LLMs do what they're good at (handle the fuzzy and imprecise nuances of natural language, which includes the scooping of a lot of context) and delegate other things they're not good at to external tools, some of which if can write on the spot. If you think about it, we humans do that all the time too…
They cannot, however, execute that code. They can feed that code into an external program they've been given access to, but they can't execute it themselves.