Skip to content

Commit

Permalink
Rewrite euler-08.c by copying euler-08a.c over it and removing euler-…
Browse files Browse the repository at this point in the history
…08a.c

Update README.md to report the new working status.
Should really disable all the printing except the answer and check with Project Euler.
Maybe add options for -v verbose and -d debug?
  • Loading branch information
jleffler committed Jul 10, 2016
1 parent e42ae25 commit c8c76d0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 147 deletions.
1 change: 0 additions & 1 deletion src/so-2624-0690/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
euler-08
euler-08a
12 changes: 6 additions & 6 deletions src/so-2624-0690/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ greatest product. What is the value of this product?
There are 20 lines of 50 digits per line in the data - see e8-02.data
for 'just the digits'.

As of 2016-07-08, the code crashes on the sequences in lines 5-6 of the
data. It also fails on lines 9-11 (but not on just 10-11, or 9-10). It
works on lines 1-5, also on lines 6-10, and also on lines 11-20. The
crash is an assertion failure that the current 13-digit product is not
divisible by the first digit in the current 13-digit sequence. It is
not yet clear what triggers this problem.
Answer:

Maximum product = 23514624000; start = 198; len = 13; digits = 5576689664895

Looking at the last 3 digits of line 4 and the first 10 of line 5
matches the printed data.
105 changes: 69 additions & 36 deletions src/so-2624-0690/euler-08.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,98 @@

enum { N_DIGITS = 13 };

static void print_digits(const char buffer[N_DIGITS], size_t f_pos, size_t n_digits)
{
for (size_t i = 0; i < n_digits; i++)
{
size_t j = (f_pos + i) % N_DIGITS;
printf("%d", buffer[j]);
}
}

static void copy_digits(char dst[N_DIGITS], const char src[N_DIGITS], size_t f_pos, size_t n_digits)
{
for (size_t i = 0; i < n_digits; i++)
{
size_t j = (f_pos + i) % N_DIGITS;
*dst++ = src[j];
}
}

int main(void)
{
int c;
char buffer[N_DIGITS];
size_t b_pos = 0;
size_t c_count = 0; // Digits since last 0
char buffer[N_DIGITS] = "";
size_t f_pos = 0; // Position in buffer
size_t n_digits = 0; // Number of digits
size_t d_count = 0; // Digits in total
size_t max_end = 0;
uint64_t product = 1; // Current product
uint64_t max_prod = 0; // Maximum product

memset(buffer, 1, sizeof(buffer));
for (size_t i = 0; i < sizeof(buffer); i++)
assert(buffer[i] == 1);
char max_buf[N_DIGITS] = "";
size_t max_len = 0;
uint64_t max_prd = 0;
size_t max_bgn = 0;

while ((c = getchar()) != EOF)
{
if (!isdigit(c))
continue;
printf("c = %c (d-count = %4zu, c-count = %4zu, b_pos = %2zu, product = %13" PRIu64,
c, d_count, c_count, b_pos, product);
if (max_prod > 0)
{
assert(max_end >= N_DIGITS);
printf(", max = %13" PRIu64 ", bgn = %4zu, end = %4zu",
max_prod, max_end - N_DIGITS + 1, max_end);
}
printf(")\n");

d_count++;
c_count++;
int digit = c - '0';
assert(digit >= 0 && digit <= 9);
if (digit == 0)
{
b_pos = 0;
buffer[b_pos] = 1;
printf("Zeroed!\n");
f_pos = 0;
product = 1;
c_count = 0;
n_digits = 0;
}
else
{
if (c_count >= N_DIGITS)
size_t w_pos = (f_pos + n_digits) % N_DIGITS;

int o_val = buffer[w_pos];
buffer[w_pos] = digit;
if (n_digits < N_DIGITS)
{
o_val = 0;
n_digits++;
}
else
{
if (product > max_prod)
{
max_prod = product;
max_end = d_count;
}
printf("bp = %zu; B[bp] = %d; product = %13" PRIu64 "\n", b_pos, buffer[b_pos], product);
assert(buffer[b_pos] != 0);
assert(product % buffer[b_pos] == 0);
product /= buffer[b_pos];
assert(o_val > 0 && o_val <= 9);
assert(product % o_val == 0);
product /= o_val;
f_pos = (f_pos + 1) % N_DIGITS;
}

product *= digit;
buffer[b_pos++] = digit;
if (b_pos >= N_DIGITS)
b_pos = 0;

printf("Digit: %d - d_count = %4zu; n_digits = %2zu; f_pos = %2zu;"
" o_val = %d; product = %13" PRIu64 "; digits = ",
digit, d_count, n_digits, f_pos, o_val, product);
print_digits(buffer, f_pos, n_digits);
putchar('\n');

if (product > max_prd)
{
max_prd = product;
max_len = n_digits;
copy_digits(max_buf, buffer, f_pos, n_digits);
max_bgn = d_count - max_len + 1;
printf("New maximum: product = %13" PRIu64 "; start = %4zu; len = %2zu; digits = ",
max_prd, max_bgn, max_len);
print_digits(max_buf, 0, max_len);
putchar('\n');
}
}
}
printf("Max product is %" PRIu64 " starting at %zu up to %zu\n",
max_prod, max_end - N_DIGITS + 1, max_end);

printf("Maximum product = %13" PRIu64 "; start = %4zu; len = %2zu; digits = ",
max_prd, max_bgn, max_len);
print_digits(max_buf, 0, max_len);
putchar('\n');

return 0;
}
104 changes: 0 additions & 104 deletions src/so-2624-0690/euler-08a.c

This file was deleted.

0 comments on commit c8c76d0

Please sign in to comment.