-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler95.c
More file actions
47 lines (44 loc) · 1019 Bytes
/
euler95.c
File metadata and controls
47 lines (44 loc) · 1019 Bytes
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
46
47
#include <stdio.h>
#define N 1000000
int a[N+1];
char b[N+1];
int main()
{
int i, j, length, small;
int longest=0, smallest=N+1;
for (i=1; i<=500000; i++) {
if (a[i]>=0) {
for (j=(i<<1); j<=N; j+=i) {
if (a[j] >= 0) {
a[j] += i;
if (a[j]>N) {
a[j] = -1;
}
}
}
}
}
for (i=2; i<=N; i++) {
j = i;
length = 1;
small = N+1;
memset(b, 0, (N+1)*sizeof(char));
while (a[j]>0 && b[a[j]]!=1) {
length ++;
if (a[j]<small) {
small = a[j];
}
if (a[j] == i) {
if (length>longest) {
longest = length;
smallest = small;
}
break;
}
b[a[j]] = 1;
j = a[j];
}
}
printf("%d %d\n", longest, smallest);
return 0;
}