I want to find an integer called result
, where its squared value (result
^2) has a pattern of 1_2_3_4_5_6_7_8_9_0 (with _ being digits). My approach is, find all numbers with such pattern and find the number with its square root being an integer:
#include <cmath>
#include <string>
using std::string;
int main(){
std::string candidate;
long result;
long maxPosibleSq = 1929394959697989990;
long minPosibleSq = 1020304050607080900;
for (long i=minPosibleSq; i<=maxPosibleSq; i+=10 /*last digit is always 0*/){
if (sqrt(i)==floor(sqrt(i))){
candidate = std::to_string(i);
if (candidate[2]=='2' && candidate[4]=='3' && candidate[6]=='4' && candidate[8]=='5' && candidate[10]=='6'
&& candidate[12]=='7' && candidate[14]=='8' && candidate[16]=='9'){
result=std::stol(candidate);
break;
}
}
}
return 0;
}
The compiler gave me warnings on potential overflows because maxPosibleSq
and minPosibleSq
are too big. Are there any better and faster ways to do it?