Skip to content

Commit e25488a

Browse files
committed
rewrite problem47 for more readability
1 parent 730e34b commit e25488a

File tree

1 file changed

+21
-39
lines changed

1 file changed

+21
-39
lines changed

40-49/problem47.c

+21-39
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,34 @@
88
#define N 4
99
#define M 1000000
1010

11+
static int distinct_factor_count(unsigned n);
12+
1113
/* this program may take sevaral seconds */
1214
int main(void)
1315
{
14-
unsigned i;
15-
for (i = 2; i < M; i++) {
16-
unsigned factors[N][N];
17-
unsigned j;
18-
19-
for (j = 0; j < N; j++) {
20-
unsigned c, k, l;
16+
unsigned i, j = 0;
17+
for (i = 2; i < M && j < N; i++) {
18+
for (j = 0; j < N && distinct_factor_count(i+j) == 4; j++)
19+
;
20+
}
21+
printf("%u\n", i-1);
2122

22-
for (c = 0; c < N; c++) {
23-
factors[j][c] = 1;
24-
}
23+
return 0;
24+
}
2525

26-
/* factorize */
27-
k = i+j;
28-
l = 2;
29-
c = 0;
30-
while (k != 1 && c < N) {
31-
while (k % l == 0) {
32-
k /= l;
33-
factors[j][c] *= l;
34-
}
35-
l++;
36-
if (factors[j][c] != 1) {
37-
c++;
38-
}
39-
}
40-
if (c != N) {
41-
/* too few factors */
42-
goto NEXT;
43-
}
44-
if (k != 1) {
45-
/* too many factors */
46-
goto NEXT;
26+
int distinct_factor_count(unsigned n)
27+
{
28+
unsigned count = 0;
29+
unsigned i;
30+
for (i = 2; i <= n; i++) {
31+
if (n % i == 0) {
32+
count++;
33+
n /= i;
34+
while (n % i == 0) {
35+
n /= i;
4736
}
4837
}
49-
50-
/* this i is the answer! */
51-
printf("%u\n", i);
52-
break;
53-
NEXT:
54-
;
5538
}
56-
57-
return 0;
39+
return count;
5840
}
5941

0 commit comments

Comments
 (0)