hi i have question about one problem show if square of number is equal sum of it's n right digit plus n or n-1 left digit? or
let n=297
n*n=88209
and 88+209=n
what is algorithm to solve this problem please help
hi i have question about one problem show if square of number is equal sum of it's n right digit plus n or n-1 left digit? or
let n=297
n*n=88209
and 88+209=n
what is algorithm to solve this problem please help
Probably convert the square of the original number to a string and then you can convert parts of that string to ints and add them. Store an int for the dividing index position and iterate over each index split position doing the test.
Of course there are many other ways to do it as well.
Here's a quick go of a right-to-left scan test for a single number using integers not strings. Untested though:
int left_digits = n*n;
int right_digits = 0;
int next_right_digit_multiplier = 1;
while ((left_digits > 0) && (right_digits < n))
{
if ((left_digits + right_digits) == n)
{
return true;
}
int next_digit = left_digits % 10;
left_digits /= 10;
right_digits += (next_digit * next_right_digit_multiplier);
next_right_digit_multiplier *= 10;
}
return false;
Note that it would probably be better to find some theory here too if you start working with really large numbers, e.g. only test the central few cases where neither side has more digits than n (ignoring leading zeroes!).