method to count the number of 2s in 0 ~ N