Concealed Square | Project Euler | Problem #206
URL to the problem page: https://projecteuler.net/problem=206
Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, where each “_” is a single digit.
Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, where each “_” is a single digit.
#include <iostream>
using namespace std;
long long int power(long long int a, long long int b) {
long long int result = 1;
for (int i = 0; i < b; i++) {
result *= a;
}
return result;
}
int main()
{
long long int i, j, digits[19] = { 0 }, a, cnt, number;
for (i = 1000000000; i <= 10000000000; i += 10) {
number = i * i;
cnt = 1;
while (number >= 10) {
number /= 10;
cnt++;
}
if (cnt != 19) {
continue;
}
number = i * i;
cnt = 0;
a = 1;
for (j = 19; j > 0; j--) {
digits[j - 1] = (number % power(10, j)) / (power(10, (j - 1)));
if (j >= 3 && (j - 1) % 2 == 0) {
if (digits[j - 1] != a) {
cnt++;
break;
}
a++;
}
}
if (cnt == 0) {
cout << "Unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, where each '_' is a single digit is = " << i << endl;
cout << i << " ^ 2 = " << number << endl;
break;
}
}
return 0;
}
Comments
Post a Comment