Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

rewrite problem47 for more readability

  • Loading branch information...
commit e25488a68a4f7f162f438655be0ee5dc8696ad4d 1 parent 730e34b
@eagletmt authored
Showing with 21 additions and 39 deletions.
  1. +21 −39 40-49/problem47.c
View
60 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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.