TheAlgorithms-C/project_euler/problem_8/sol1.c

117 lines
3.3 KiB
C
Raw Normal View History

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 02:08:59 +03:00
#include <stdio.h>
#include <stdlib.h>
2020-06-05 19:20:25 +03:00
/** Compute the product of two numbers in a file
*
* \param[in] fp pointer to file that is already open
* \param[in] start_pos line number of the first numer
* \param[in] num_digits number of digits on the line to multiply
* \returns expected product
*/
long long int get_product(FILE *fp, long start_pos, int num_digits)
2020-03-30 02:08:59 +03:00
{
char ch = ' '; /* temporary variable to store character read from file */
2020-04-08 16:59:44 +03:00
unsigned char num = 0; /* temporary variable to store digit read */
long long int prod = 1; /* product accumulator */
int count =
0; /* we use this variable to count number of bytes of file read */
2020-03-30 02:08:59 +03:00
/* accumulate product for num_digits */
2020-04-07 07:39:42 +03:00
for (int i = 0; i < num_digits; i++, count++)
2020-03-30 02:08:59 +03:00
{
/* get character from file */
ch = getc(fp);
2020-04-07 07:39:42 +03:00
/* the ASCII codes of digits is between 0x30 and 0x39.
2020-03-30 02:08:59 +03:00
* any character not in this range implies an invalid character
*/
if (ch < 0x30 || ch > 0x39)
{
if (ch == EOF)
return 0;
i--;
continue;
}
2020-04-07 07:39:42 +03:00
2020-03-30 02:08:59 +03:00
num = ch - 0x30; /* convert character digit to number */
if (num == 0)
{
2020-04-07 07:39:42 +03:00
/* If number is zero, we can skip the next 'num_digits'
* because this '0' will repeat in the next 'num_digit'
* multiplications. Hence, we also do not update the file position
*/
2020-03-30 02:08:59 +03:00
/* NOTE: this is not needed but helps get results faster :) */
return 0;
}
2020-04-07 07:39:42 +03:00
prod *= num; /* accumulate product */
2020-03-30 02:08:59 +03:00
}
/* set file position to the next starting character + 1 */
fseek(fp, -count + 1, SEEK_CUR);
2020-04-07 07:39:42 +03:00
2020-03-30 02:08:59 +03:00
return prod;
}
2020-06-05 19:20:25 +03:00
/** Main function */
2020-04-07 07:39:42 +03:00
int main(int argc, char *argv[])
2020-03-30 02:08:59 +03:00
{
int position = 0;
int num_digits = 4;
long long int prod, max_prod = 0;
2020-03-30 02:08:59 +03:00
/* if second command-line argument is ge=iven,
2020-04-07 07:39:42 +03:00
* use it as the number of digits to compute
2020-03-30 02:08:59 +03:00
* successive product for
*/
if (argc == 2)
num_digits = atoi(argv[1]);
/* open file to read digits from */
FILE *fp = fopen("digits.txt", "rt");
if (!fp)
{
perror("Unable to open file");
return -1;
}
/* loop through all digits in the file */
2020-04-07 07:39:42 +03:00
do
2020-03-30 02:08:59 +03:00
{
/* get product of 'num_digits' from current position in file */
prod = get_product(fp, ftell(fp), num_digits);
2020-04-07 07:39:42 +03:00
2020-03-30 02:08:59 +03:00
if (prod > max_prod)
{
max_prod = prod;
position = ftell(fp) - 1;
}
2020-04-07 07:39:42 +03:00
} while (!feof(fp)); /* loop till end of file is reached */
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 02:08:59 +03:00
/* loop through all digits */
for (; num_digits > 0; num_digits--)
{
2020-04-07 07:39:42 +03:00
char ch = getc(fp); /* get character */
2020-03-30 02:08:59 +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:39:42 +03:00
fclose(fp); /* close file */
2020-03-30 02:08:59 +03:00
return 0;
}