I used to think this, then I worked at a telco for a while.. The problem is a lot more complicated..
Due to how ILECs and CLECs (incumbent/parent phone companies who own the infrastructure and then wholesalers/resellers who operate on top of them due to competition laws) integrate, how they share and allocate phone number prefixes and lists (this whole process alone is mind-blowingly arcane), and how all phone companies cooperate internationally, it's extremely hard - almost impossible - for them to identify and block these kinds of nuisance calls in real-time.
And don't get me wrong, they could do it.. But the amount of coordination and cooperation that would be required is a non-starter because most phone companies don't talk to each other enough to get this kind of initiative off the ground, even if the money and motivation was there.
Edit: Remember that most (Western) phone companies are 100-year-old institutions, and many still have 50+ year-old infrastructure that runs the underlying systems, and that they can't just throw that away and start over, due to how deeply integrated they are in the public/private communication infrastructure of society, and many of their modern systems are really just shims built on top of that older tech.
Breaking up AT&T into regional bell operating companies[1] (the ILEC thing) was a very stupid decision in hindsight. I don't know what actually happened. Did AT&T simply have smarter people than the US Government did?