I don't know why if I have the following code:
int main() {
long int height_cat, number_worker_cats, number_helper_cats, height_tree;
bool flag;
scanf("%ld%ld", &height_cat, &number_worker_cats);
for (number_helper_cats = 1; ; ++number_helper_cats) {
for (height_tree = 1; (long int)pow(number_helper_cats + 1, height_tree) <= height_cat; ++height_tree) {
if ((long int)(pow(number_helper_cats + 1, height_tree) - height_cat) == 0 && (long int)(pow(number_helper_cats, height_tree) - number_worker_cats) == 0) {
flag = true;
break;
}
}
if (flag) {
break;
}
}
printf("%ld, %ld\n", number_helper_cats, height_tree);
}
I'm searching for number_helper_cats
and height_tree
which (number_helper_cats +1)^height_tree = height_cat
and number_helper_cats^height_tree = number_worker_cats
where height_cat
and number_worker_cats
are integers
.
For example if height_cat = 216
and number_worker_cats = 125
, the code will stop on number_helper_cats = 5
and height_tree = 3
since (5+1)^3 = 216
and 5^3 = 125
.
But if I have the following code it doesnt work, loops forever, why?
int main() {
long int height_cat, number_worker_cats, number_helper_cats, height_tree;
bool flag;
scanf("%ld%ld", &height_cat, &number_worker_cats);
for (number_helper_cats = 1; ; ++number_helper_cats) {
for (height_tree = 1; pow(number_helper_cats + 1, height_tree) <= height_cat; ++height_tree) {
if ((long int)(pow(number_helper_cats + 1, height_tree)) == height_cat &&
(long int)(pow(number_helper_cats, height_tree)) == number_worker_cats) {
flag = true;
break;
}
}
if (flag) {
break;
}
}
printf("%ld, %ld\n", number_helper_cats, height_tree);
}
Everything is long int
and every height_cat and number_worker_cats for testcase are true for the operations, another example height_cat = 5764801, number_worker_cats = 1679616, number_helper_cats = 6 and height_tree = 8 because (6 + 1)^8 = 5764801, 6^8 = 1679616. But again the first code runs well, and the second one loops forever. pow are precise I mean 6^3 = 216 and 5^3 = 125 right? :p