Here's a challenge - I am confident from the information in the thread you'll be able to get your timing down, whether it's enough I don't know - it doesn't compromise @Meden's first call or the Project Euler spirit ;)
#include <iostream>
#include <algorithm>
#include <chrono>
usingnamespace std;
// The official timer, apparently
struct Timer
{
Timer() : beg_(clock_::now()) {}
void reset() { beg_ = clock_::now(); }
double elapsed() const { return std::chrono::duration_cast<second_> (clock_::now() - beg_).count(); }
using clock_= std::chrono::high_resolution_clock;
using second_= std::chrono::duration<double, std::ratio<1>>;
std::chrono::time_point<clock_> beg_;
};
int main()
{
Timer tmr;
int left = 999, right = 999;
while ( left > 0 )
{
int test = 1000 * left + right;
for ( int small = max( test / 999, 100 ); small <= 999; small++ )
{
int large = small;
while ( large <= 999 )
{
int n = small * large;
if ( n == test )
{
double t = tmr.elapsed();
cout << small << " x " << large << " = " << test << '\n';
cout << "Processor time taken = " << t;
return 0;
}
elseif ( n > test ) break;
large++;
}
}
left--;
if ( right < 10 ) right += 989;
elseif ( right < 100 ) right += 890;
else right -= 100;
}
}
Timing the code with stringstream-generated palindromes, plus my bloopers earlier on in this thread are enough to make me very cautious using stringstream in future.
Just using strings (with to_string() and reverse iterators) was a little faster, but didn't match integer arithmetic alone.
> Timing the code with stringstream-generated palindromes, plus my bloopers earlier on in this
> thread are enough to make me very cautious using stringstream in future.