I'm writing a program that will solve problem 4 in project euler(Find the largest palindrome made from the product of two 3-digit numbers) and I've gotten everything down except this one bug that's stopped me in my tracks. Here's the function it's in:
int processingLoop()
{
const int MIN = 100;
int solution = 0;
int product = 0;
for(int numOne = 1; numOne < MIN; numOne++)
{
for(int numTwo = 1; numTwo < MIN; numTwo++)
{
product = numOne * numTwo;
if (reverseNumber() == product)
{
std::cout << solution << '\n';
}
}
}
std::cout << "No solution\n";
return 0;
}
The purpose of this function is that it runs through a loop of multiplying a pair of two digit numbers in every combination possible, and it should result in the solution printing as "9009". The problem comes from the if statement inside of the nested for loop. Having that if statement there with any sort of representation of the function reverseNumber() leads to the program skipping through the if statement and breaking out of the for loop to print "No solution". Is this undefined behavior? Or is this some sort of weird rule I haven't heard of?
Aucun commentaire:
Enregistrer un commentaire