Fix #6262

Use an Int for the accumulation variable when iterating over integer types smaller than Int, and the type of the range when iterating over types larger than Int