You are viewing a single comment's thread. Return to all comments →
C++ Solution:
int count = 0; for(int i = p; i <= q; i++) { unsigned long squared = (unsigned long) i*i; string str = to_string(squared); int str_size = str.length(); if(str_size == 1) { if(squared == (unsigned long)i) { cout << i << " "; count++; } } else if(str_size > 1) { //cout << "Squared: " << squared << ", " << stoi(str.substr(0,str_size/2)) << ", " << stoi(str.substr(str_size/2,str_size-1)) << endl; int left = stoi(str.substr(0,str_size/2)); int right = stoi(str.substr(str_size/2,str_size-1)); //cout << "Left: " << left << ", right: " << right << endl; int sum = left + right; if(sum == i) { cout << i << " "; count++; } } } if(count == 0) cout << "INVALID RANGE\n";
Seems like cookies are disabled on this browser, please enable them to open this website
Modified Kaprekar Numbers
You are viewing a single comment's thread. Return to all comments →
C++ Solution: