The problem from here: find nbAdjacent digits in nStr with highest product. So I stored the number as a string and everything my code prints is correct yet the final result is wrong, I can't figure out why... I've continued Project Euler in the meantime but this is bugging me.
Here's my code:
std::string nStr = "731...50"; // a 1000-digit number
uint64_t prod = 1;
int nbAdjacent = 13;
int max = 0;
for (int i = 0; i <=988; i++)
{
for (int j = 0; j < nbAdjacent; j++)
{
// yes i thought about that ;)
prod *= nStr[i + j] - '0';
}
if (prod > max)
{
max = prod;
for (int j = 0; j < nbAdjacent-1; j++)
std::cout << nStr[i + j] << " * ";
// each new max is printed
std::cout << nStr[i + nbAdjacent - 1] << " = " << prod << std::endl;
}
prod = 1;
}
and it prints this:
7 * 3 * 1 * 6 * 7 * 1 * 7 * 6 * 5 * 3 * 1 * 3 * 3 = 5000940
4 * 9 * 1 * 9 * 2 * 2 * 5 * 1 * 1 * 9 * 6 * 7 * 4 = 9797760
9 * 2 * 2 * 5 * 1 * 1 * 9 * 6 * 7 * 4 * 4 * 2 * 6 = 13063680
2 * 5 * 1 * 1 * 9 * 6 * 7 * 4 * 4 * 2 * 6 * 5 * 7 = 25401600
5 * 1 * 1 * 9 * 6 * 7 * 4 * 4 * 2 * 6 * 5 * 7 * 4 = 50803200
1 * 1 * 9 * 6 * 7 * 4 * 4 * 2 * 6 * 5 * 7 * 4 * 7 = 71124480
1 * 9 * 6 * 7 * 4 * 4 * 2 * 6 * 5 * 7 * 4 * 7 * 4 = 284497920
9 * 6 * 7 * 4 * 4 * 2 * 6 * 5 * 7 * 4 * 7 * 4 * 2 = 568995840
5 * 3 * 4 * 9 * 1 * 9 * 4 * 9 * 3 * 4 * 9 * 6 * 9 = 1020366720
3 * 4 * 9 * 1 * 9 * 4 * 9 * 3 * 4 * 9 * 6 * 9 * 8 = 1632586752
9 * 1 * 9 * 4 * 9 * 3 * 4 * 9 * 6 * 9 * 8 * 3 * 5 = 2040733440
8 * 6 * 9 * 4 * 7 * 8 * 8 * 5 * 1 * 8 * 4 * 3 * 8 = 2972712960
I checked for index error in the for loops but found nothing wrong...