2020-06-05 19:20:25 +03:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief [Problem 8](https://projecteuler.net/problem=8) solution
|
2020-06-06 21:51:49 +03:00
|
|
|
* \author [Krishna Vedala](https://github.com/kvedala)
|
2020-06-05 19:20:25 +03:00
|
|
|
*/
|
2020-03-30 03:10:23 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-04-07 07:41:14 +03:00
|
|
|
#include <string.h> /* for memmove */
|
2020-03-30 03:10:23 +03:00
|
|
|
|
2020-06-05 19:20:25 +03:00
|
|
|
/** Main function */
|
2020-04-07 07:41:14 +03:00
|
|
|
int main(int argc, char *argv[])
|
2020-03-30 03:10:23 +03:00
|
|
|
{
|
|
|
|
int position = 0, num_bad_chars = 0;
|
|
|
|
int num_digits = 4;
|
|
|
|
char ch;
|
2020-04-08 16:59:44 +03:00
|
|
|
unsigned char num, num_prev;
|
|
|
|
unsigned char *buffer = NULL;
|
2020-04-08 16:48:04 +03:00
|
|
|
long long int prod = 1, max_prod = 0;
|
2020-03-30 03:10:23 +03:00
|
|
|
|
|
|
|
/* if second command-line argument is given,
|
2020-04-07 07:41:14 +03:00
|
|
|
* use it as the number of digits to compute
|
2020-03-30 03:10:23 +03:00
|
|
|
* successive product for
|
|
|
|
*/
|
|
|
|
if (argc == 2)
|
|
|
|
num_digits = atoi(argv[1]);
|
2020-04-07 07:41:14 +03:00
|
|
|
|
2020-03-30 03:10:23 +03:00
|
|
|
/* allocate memory to store past values */
|
2020-04-08 17:02:08 +03:00
|
|
|
buffer = calloc(num_digits, sizeof(unsigned char));
|
2020-04-07 07:41:14 +03:00
|
|
|
if (!buffer)
|
2020-03-30 03:10:23 +03:00
|
|
|
{
|
|
|
|
perror("Unable to allocate memory for buffer");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open file to read digits from */
|
|
|
|
FILE *fp = fopen("digits.txt", "rt");
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
perror("Unable to open file");
|
2020-04-07 07:41:14 +03:00
|
|
|
free(buffer); /* free allocated memory */
|
2020-03-30 03:10:23 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loop through all digits in the file */
|
2020-04-07 07:41:14 +03:00
|
|
|
do
|
2020-03-30 03:10:23 +03:00
|
|
|
{
|
|
|
|
/* get character from file */
|
|
|
|
ch = getc(fp);
|
|
|
|
|
2020-04-07 07:41:14 +03:00
|
|
|
/* the ASCII codes of digits is between 0x30 and 0x39.
|
2020-03-30 03:10:23 +03:00
|
|
|
* any character not in this range implies an invalid character
|
|
|
|
*/
|
|
|
|
if (ch < 0x30 || ch > 0x39)
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
num_bad_chars++; /* this is used to get the bad characters in the
|
|
|
|
sequence of 13 characters */
|
2020-03-30 03:10:23 +03:00
|
|
|
continue;
|
2020-04-07 07:41:14 +03:00
|
|
|
}
|
|
|
|
else if (num_bad_chars > 0)
|
|
|
|
num_bad_chars--;
|
|
|
|
|
|
|
|
num = ch - 0x30; /* convert character digit to number */
|
|
|
|
num_prev = buffer[0]; /* previous n^th digit */
|
2020-03-30 03:10:23 +03:00
|
|
|
|
|
|
|
/* left shift the buffer -
|
|
|
|
* using a for loop or a faster memory move
|
|
|
|
*/
|
2020-04-07 07:41:14 +03:00
|
|
|
memmove(buffer, buffer + 1, num_digits - 1);
|
2020-03-30 03:10:23 +03:00
|
|
|
/*
|
|
|
|
for (int i = 1; i < num_digits; i++)
|
|
|
|
buffer[i-1] = buffer[i];
|
|
|
|
*/
|
|
|
|
|
2020-04-07 07:41:14 +03:00
|
|
|
buffer[num_digits - 1] = num; /* save the latest number in buffer */
|
2020-03-30 03:10:23 +03:00
|
|
|
|
|
|
|
if (num_prev != 0)
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
/* since product is accumulated, the new product can be obtained by
|
|
|
|
* simply multiplying the new digit and dividing with the oldest
|
|
|
|
* digit
|
2020-03-30 03:10:23 +03:00
|
|
|
*/
|
2020-04-07 07:41:14 +03:00
|
|
|
prod /= num_prev; /* divide first to avoid over-flows */
|
2020-03-30 03:10:23 +03:00
|
|
|
prod *= num;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prod = 1;
|
2020-04-07 07:41:14 +03:00
|
|
|
for (int i = 0; i < num_digits; i++)
|
2020-03-30 03:10:23 +03:00
|
|
|
{
|
2020-04-07 07:41:14 +03:00
|
|
|
if (buffer[i] == 0)
|
2020-03-30 03:10:23 +03:00
|
|
|
{
|
|
|
|
prod = 0;
|
2020-04-07 07:41:14 +03:00
|
|
|
break; /* break innermost for-loop */
|
2020-03-30 03:10:23 +03:00
|
|
|
}
|
|
|
|
prod *= buffer[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if a new maxima was found */
|
|
|
|
if (prod > max_prod)
|
|
|
|
{
|
|
|
|
max_prod = prod;
|
|
|
|
position = ftell(fp) - num_bad_chars - num_digits - 1;
|
|
|
|
}
|
2020-04-07 07:41:14 +03:00
|
|
|
} while (!feof(fp)); /* loop till end of file is reached */
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Maximum product: %lld\t Location: %d^th position\n\t", max_prod,
|
|
|
|
position);
|
|
|
|
fseek(fp, position,
|
|
|
|
SEEK_SET); /* move cursor to identified position in file */
|
2020-03-30 03:10:23 +03:00
|
|
|
/* loop through all digits */
|
|
|
|
for (; num_digits > 0; num_digits--)
|
|
|
|
{
|
2020-04-07 07:41:14 +03:00
|
|
|
char ch = getc(fp); /* get character */
|
2020-03-30 03:10:23 +03:00
|
|
|
/* skip invalid character */
|
|
|
|
if (ch < 0x30 || ch > 0x39)
|
|
|
|
continue;
|
|
|
|
if (num_digits > 1)
|
|
|
|
printf("%c x ", ch);
|
|
|
|
else
|
|
|
|
printf("%c = %lld\n", ch, max_prod);
|
|
|
|
}
|
|
|
|
|
2020-04-07 07:41:14 +03:00
|
|
|
fclose(fp); /* close file */
|
|
|
|
free(buffer); /* free allocated memory */
|
2020-03-30 03:10:23 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|