Mohammed, the people who told the story of Ali Baba and the ancient Jews all spoke Semitic languages. As with a lot of metaphors, it might have come with the language. Like how early Indo-European cultures had patrilocality and sky gods
Things like these can jump across language and culture boundaries easily, too.
In Russian, there's a fairly obvious pattern to numerals like 20, 30, 50, 60, 70, 80, 90. It's not super consistent, but you can easily pick each of those apart and see that it references the corresponding digit, plus some variation of "ten".
Note that I didn't list 40. That's because it's special - it has its own word for it, that is completely unrelated to the word for 4 or any other number. Its etymology is contested, but it's certain that it was already special many centuries back, and used to count things (in groups of 40). And then you have archaic idioms like "forty forties", which basically means "hell of a lot".
Of course, it can well just be convergent evolution - there's only so many round numbers. And then if you pick one as the standard size of the group for counting, and it's large enough, then a square of that number is also a fairly natural choice for "many". But identical choices in other cultures might give the initial push, or reinforce one of the early roughly equal candidates.
Exist many samples of the use of numbers in the thousands, btw..