Conditions for Length Generalization in Learning Reasoning Skills