Phylogeny, ecology, and algorithms in the origins of numerical cognition