blob: 8616cf8c817e0f676457eb3daac551a7241a8ee7 (
plain)
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
39
40
41
42
43
44
45
|
const int limit = 10000000;
const int squares[10] = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 };
int next_chain(int number)
{
int sum = 0;
int digit;
while(number > 0)
{
digit = number % 10;
number = number / 10;
sum += squares[digit];
}
return sum;
}
int main()
{
int* numbers = calloc(limit, sizeof(int));
int i, count;
for(i=0; i<limit; i++)
numbers[i] = next_chain(i);
count = 0;
for(i=1; i<limit; i++)
{
int next = numbers[i];
while(next != 1 && next != 89)
next = numbers[next];
numbers[i] = next;
if(next == 89)
count++;
}
printf("%d\n", count);
return 0;
}
|