Skip to content

Commit

Permalink
Problem 74: Digit factorial chains
Browse files Browse the repository at this point in the history
  • Loading branch information
pbevin committed Dec 27, 2013
1 parent 9d270a8 commit 20d2fae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
43 changes: 43 additions & 0 deletions eu074.c
@@ -0,0 +1,43 @@
#include "euler.h"

static int digfact[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };

static int factsum(int n) {
int t = 0;
while (n > 0) {
int d = n % 10;
t += digfact[d];
n /= 10;
}
return t;
}

#define N 1000000
#define LEN 60

void eu074(char *ans) {
int count = 0;

for (int i = 1; i < N; i++) {
int chain[LEN+1];
chain[0] = i;
int n = 1, v = i;
int done = 0;
while (n <= LEN) {
v = factsum(v);

for (int j = 0; j < n-1; j++) {
if (chain[j] == v) {
done = 1;
break;
}
}
if (done) break;

chain[n++] = v;
}

if (n == LEN) count++;
}
sprintf(ans, "%d", count);
}
1 change: 1 addition & 0 deletions euler.c
Expand Up @@ -295,6 +295,7 @@ struct puzzle puzzles[] = {
{ "071", &eu071, "428570" },
{ "072", &eu072, "303963552391" },
{ "073", &eu073, "7295372" },
{ "074", &eu074, "402" },
};

#define NPUZZLES (sizeof puzzles / sizeof(puzzles[0]))
Expand Down

0 comments on commit 20d2fae

Please sign in to comment.