In last iteration of the loop result is wrong. I know that before subtraction numbers can be bigger than long. That is why I set power to long long. Result in last iteration should be 17888888888888888889. Why it is not?
const int NR_LEVELS = 18;
unsigned long levels[NR_LEVELS];
unsigned long long power = 10;
for(unsigned int i = 0; i < NR_LEVELS; i++) {
levels[i] = ((i+1)*10*power-(i+2)*power+1)/9;
cout << levels[i] << endl;
power *= 10;
}
levels[17] = 17888888888888888889lu;
for(unsigned int i = 0; i < NR_LEVELS; i++) {
cout << levels[i] << endl;
}