I am trying to solve the following problem: https://projecteuler.net/problem=8
Basically I am going through the 2D array and getting the 13 digit product from each of the eight directions (N, NW, NE, E, W, S, SW, SE) and setting the return value to the largest one (if there is one).
But I am getting an answer of 64497254400, which is incorrect. Any hints as to what I could possibly be doing wrong?
Following is my straightforward, inefficient solution:
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
long long int MAX(long long int a, long long int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
void largest_product_in_series(int param)
{
char filename[] = "8_.txt";
char my_character ;
int column = 0; int row = 0;
int c = 0; int r = 0; int cc = 0; int rr = 0;
int list[100][100];
int num = param - 1;
ifstream fin;
fin.open(filename, ios::in);
while (!fin.eof() )
{
fin.get(my_character);
if (my_character == '\n')
{
row++;
r++;
c = 0;
cout << endl;
}
else
{
if (row == 0)
{
column++;
}
list[r][c] = (my_character - '0');
cout << list[r][c];
c++;
}
}
cout << "Column: " << column << endl;
cout << "Row: " << row << endl;
long long int greatest_product = 0;
long long int product = 0;
for (rr = 0; rr < row; rr++)
{
for (cc = 0; cc < column; cc++)
{
//cout << "Column: " << cc << " Row: " << rr << endl;
if (rr >= num)
{
r = rr;
c = cc;
product = list[r--][c];
for (int i = 0; i < num; i++)
{
product *= list[r--][c];
}
greatest_product = MAX(greatest_product, product);
}
if ((rr + num) <= row)
{
r = rr;
c = cc;
product = list[r++][c];
for (int i = 0; i < num; i++)
{
product *= list[r++][c];
}
greatest_product = MAX(greatest_product, product);
}
if (cc >= num)
{
r = rr;
c = cc;
product = list[r][c--];
for (int i = 0; i < num; i++)
{
product *= list[r][c--];
}
greatest_product = MAX(greatest_product, product);
}
if ((cc + num) <= column)
{
r = rr;
c = cc;
product = list[r][c++];
for (int i = 0; i < num; i++)
{
product *= list[r][c++];
}
greatest_product = MAX(greatest_product, product);
}
if ((rr >= num) && (cc >= num)) // NW
{
r = rr;
c = cc;
product = list[r--][c--];
for (int i = 0; i < num; i++)
{
product = list[r--][c--];
}
greatest_product = MAX(greatest_product, product);
}
if ((rr >= num) && ((cc + num) <= column)) // NE
{
r = rr;
c = cc;
product = list[r--][c++];
for (int i = 0; i < num; i++)
{
product = list[r--][c++];
}
greatest_product = MAX(greatest_product, product);
}
if (((rr + num) <= row) && ((cc >= num))) // SW
{
r = rr;
c = cc;
product = list[r++][c--];
for (int i = 0; i < num; i++)
{
product = list[r++][c--];
}
greatest_product = MAX(greatest_product, product);
}
if (((rr + num) <= row) && ((cc + num) <= column)) // SE
{
r = rr;
c = cc;
product = list[r++][c++];
for (int i = 0; i < num; i++)
{
product = list[r++][c++];
}
greatest_product = MAX(greatest_product, product);
}
//cout << "G: " << greatest_product << endl;
}
}
cout << "Greatest Product: " << greatest_product << endl;
}
int main(void)
{
largest_product_in_series(13);
return 0;
}