I am stuck with my solution to problem 4 of Project Euler, I have the following code which should work and it does iterate through the solution to the problem as I have researched and discovered (993*913):
// Michael Clover
// Project Euler
// Problem 4
/* A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers. */
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
bool achieved = false; // change to true when the palindrome is found
string string_conversion = "";
string first_half = ""; // first half of string
string second_half = ""; // second half of string
stringstream conversion; // use this to convert integers to strings
int check(string first_half, string second_half) {
if (first_half.compare(second_half) == 0) {
achieved = true;
return 0;
}
else {
return 0;
}
return 0;
}
int convert(int result) {
char temp;
conversion << result;
conversion >> string_conversion;
if (string_conversion.size() == 6) {
temp = string_conversion.at(0);
//cout << "temp = " << temp << endl;
first_half+=(temp);
temp = string_conversion.at(1);
//cout << "temp = " << temp << endl;
first_half+=(temp);
temp = string_conversion.at(2);
//cout << "temp = " << temp << endl;
first_half+=(temp);
temp = string_conversion.at(5);
//cout << "temp = " << temp << endl;
second_half+=(temp);
temp = string_conversion.at(4);
//cout << "temp = " << temp << endl;
second_half+=(temp);
temp = string_conversion.at(3);
//cout << "temp = " << temp << endl;
second_half+=(temp);
//cout << first_half << second_half << endl;
check(first_half, second_half);
}
//if (string_conversion.size() == 5) {
//cout << "The size of the string is 5" << endl;
//exit(1);
//}
string_conversion = "";
cout << first_half << endl;
cout << second_half << endl;
first_half.clear();
second_half.clear();
conversion.clear();
conversion.str("");
//cout << "conversion: " << conversion << endl;
return 0;
}
int iterate(int operator_one, int operator_two) { // takes two numbers and iterates through them, each time it is iterated, the result is passed to the convert function to convert to string
int two = operator_two;
for (int i = operator_one; i > 100; i--) {
int result = i * two;
cout << i << "x" << two << endl;
convert(result);
}
return 0;
}
int main() { // Use the stringstream to convert the numerical values into strings which you can then use to check if they are palindromes
int operator_one = 999;
int operator_two = 999;
while (achieved == false) {
for (int i = operator_two; i > 100; i--) {
iterate(999, i);
}
}
cout << "The largest palindrome made from the product of two 3-digit numbers is: " << string_conversion << endl;
return 0;
}
This program iterates through all of the numbers through 999×999 downwards and then splits the 6-digit numbers into two strings with the second half of the result being arranged from back to front. As shown in the console using cout << during runtime, the program tries 993*913 and both the second_half string and the first_half string contain 906. What I thought the program should then do is perform the check(string first_half, string second_half) function after iterating this and decide that both of the strings match (which should according to various sources return 0) this should then initiate the if statement within check() and set the boolean achieved to true ending the program within the main statement and printing the result before exiting the program. It does not do this however and this is my problem. Thank you for any and all help.
Leaving aside some other issues I see in this code, I think your termination problem is because you only check
achievedafter the outer loop inmain()is finished. So the inner loop (iterate()) will continue untiloperator_onefalls below 100 and then the outer loop will continue untiloperator_twofalls below 100 before you actually check your termination condition.