Ah that would explain those damn red lines. Thanks for the catch though!
Anyway, got the reversing to work, but the comparison isn't working. Here's the updated function.
bool is_palindrone(unsignedlongint n)
{
//Int to string
std::string strProd = "";
std::stringstream ss;
ss << n;
strProd = ss.str();
//Creates reversed copy.
std::string reversed;
int sz = strProd.size();
for(int i = sz; i > -1; i--)
{
reversed.push_back(strProd[i]);
std::cout << reversed << "\n";
}
//Checks for palindrone
if(strProd.compare(reversed) == 0)
{
returntrue;
}
else
{
returnfalse;
}
}
Running it prints out each step of the reverse, and the end product is the reversed copy of the original, so some reason the check at the end is failing.
EDIT:
When trying to actually output it like I have now, in the original way, it caused a crash. Probably because on iteration one, the string is uninitialized. I'm assuming, if someone wants to shed some light there that would be cool
Hmm, we seem to accomplish the same thing in different ways. I dont know if you saw, but I added some output to check to make sure it's getting reversed, and it is.
I had that thought when I started it, but had never done and only had a little bit of time left in the day. After I get this done, im gonna go back and do it that way. I feel like its gonna be ugly though
Edit: Passed a string to omit the stringstream stuff. Obviously now it'll work on any characters, not just digits. For only digits, some sort of isdigit check could be included before checking the string.
Figured it out. I'm pretty sure I was grabbing the \0 character at the end of the primary string, and sticking it in the first spot of the reversed string.
1 2 3 4 5 6 7 8
//Creates reversed copy.
std::string reversed;
int sz = strProd.size();
for(int i = sz-1; i > -1; i--)
{
reversed.push_back(strProd[i]);
std::cout << reversed << "\n";
}
Changed it to start at sz-1
ASFAIK, this works. It's worked for the few test cases I've given it. Let me know what you think/where it can be improved.
Cool, finished Euler #4. Was pretty easy. Though, what I have right now seems pretty inefficient. It only takes me 2.8 seconds, but I know there is a lot of unneccesary calculations in there. Here's what I got:
Any ideas on how to cut down on the calculations that aren't needed? Originally, I had it stop at the first palindrome it found, but that wasn't right. And I see why now.