-
Notifications
You must be signed in to change notification settings - Fork 0
/
p20.cc
38 lines (37 loc) · 924 Bytes
/
p20.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#define NDIGITS 320
unsigned int digits[NDIGITS];
void addNumAtLoc(unsigned int num, unsigned int i) {
int offset = 0;
while (num) {
if (offset == 0)
digits[i] = num%10;
else {
digits[i - offset] += num%10;
if (digits[i - offset] >= 10)
addNumAtLoc(digits[i - offset], i - offset);
}
num /= 10;
offset++;
}
}
int main(int argc, char const *argv[]) {
for (unsigned int j = 1; j < NDIGITS; ++j) {
digits[j] = 0;
}
digits[NDIGITS-1] = 1;
for (unsigned int i = 1; i <= 100; ++i) {
for (int j = 0; j < NDIGITS; ++j) {
if(digits[j] == 0) continue;
unsigned int temp = digits[j] * i;
addNumAtLoc(temp, j);
}
unsigned int sum = 0;
printf("%d!\t", i);
for (unsigned int j = 0; j < NDIGITS; ++j) {
sum += digits[j];
//printf("%u", digits[j]);
}
printf("\tsum of digits %u\n", sum);
}
return 0;
}