Skip to main content

Project Euler Solution 8

Problem Statement#

Largest product in a series.

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Solution#

In this problem the input is a 1000 digit number, so what I did was copied this 1000 digit number to a notepad. Then read the file string by string converting it into integer. Actually what happened was it converted each number into an ASCII number, so I subtracted 48 from each ASCII number to get the decimal number and stored it in an array.

Now the array with 1000 numbers is ready for finding the greatest product of five consecutive digits. For this let us consider a 10 digit number

73167176531

The following shows the array representation of this number

0123456789
7316717653

Let us see how to find the greatest product of 5 consecutive numbers

Initialize Product = 0, whenever New_product is calculated and is greater than Product, update the Product.

First iteration, i = 0

0123456789
7316717653

New_Product =7 x 3 x 1 x 6 x 7=882;

Here the New_Product > Product, so Product=882;

Second Iteration, i = 2

0123456789
7316717653

New_Product = 3 x 1 x 6 x 7 x 1 = 126;

Here New_Product < Product, So product remains the same.

Third Iteration,

0123456789
7316717653

New_Product = 1 x 6 x 7 x 1 x 7 = 294

Fourth Iteration, i = 3

0123456789
7316717653

New_Product = 6 x 7 x 1 x 7 x 6 = 1764

Update Product = 1764

Fifth Iteration, i = 4

0123456789
7316717653

New_Product = 7 x 1 x 7 x 6 x 5 = 1479;

Sixth Iteration, i = 5

0123456789
7316717653

New_Product = 1 x 7 x 6 x 5 x 3 = 630

When the product is updated for the last time in 3rd iteration, the value is 1764. So the greatedt product of five consecutive numbers in the above 10 digit number is 1764.

Always try to solve the smaller version of the bigger problem, which makes solving bigger problems easier.

Implementation#

#include <stdio.h>#include <stdlib.h>#include <string.h>
using namespace std;
int index, k, string_length, Product, New_product, i, j;int number[1000] = { 0 };char string_digit[50];FILE * f_read;
int main(){    Product = 1;    index = 0;    f_read = fopen("inputpe08.txt", "r");    while (fscanf(f_read, "%s", string_digit) == 1)    {        string_length = strlen(string_digit);        for (k = 0; k < string_length; k++)        {            number[index] = string_digit[k] - 48;            index++;        }    }    fclose(f_read);    for (i = 0; i <= 995; i++)    {        New_product = 1;        for (j = i; j <= i + 4; j++) New_product = New_product *number[j];        if (New_product >= Product) Product = New_product;    }    printf("%d", Product);    return 0;}

Sample Output#

Sample Output