# eagletmt/project-euler-c

rewrite problem47 for more readability

• Loading branch information...
1 parent 730e34b commit e25488a68a4f7f162f438655be0ee5dc8696ad4d committed Jun 6, 2009
Showing with 21 additions and 39 deletions.
1. +21 −39 40-49/problem47.c
 @@ -8,52 +8,34 @@ #define N 4 #define M 1000000 +static int distinct_factor_count(unsigned n); + /* this program may take sevaral seconds */ int main(void) { - unsigned i; - for (i = 2; i < M; i++) { - unsigned factors[N][N]; - unsigned j; - - for (j = 0; j < N; j++) { - unsigned c, k, l; + unsigned i, j = 0; + for (i = 2; i < M && j < N; i++) { + for (j = 0; j < N && distinct_factor_count(i+j) == 4; j++) + ; + } + printf("%u\n", i-1); - for (c = 0; c < N; c++) { - factors[j][c] = 1; - } + return 0; +} - /* factorize */ - k = i+j; - l = 2; - c = 0; - while (k != 1 && c < N) { - while (k % l == 0) { - k /= l; - factors[j][c] *= l; - } - l++; - if (factors[j][c] != 1) { - c++; - } - } - if (c != N) { - /* too few factors */ - goto NEXT; - } - if (k != 1) { - /* too many factors */ - goto NEXT; +int distinct_factor_count(unsigned n) +{ + unsigned count = 0; + unsigned i; + for (i = 2; i <= n; i++) { + if (n % i == 0) { + count++; + n /= i; + while (n % i == 0) { + n /= i; } } - - /* this i is the answer! */ - printf("%u\n", i); - break; -NEXT: - ; } - - return 0; + return count; }

#### 0 comments on commit `e25488a`

Please sign in to comment.