Back to Problem Solutions forum
Hi, I'm trying to solve the problem and I got a problem that I get infinite loop on some numbers, I mean my code works on 0001 and 4100 for example and probably for other numbers with small amount of loops, but for 5761 for example I get infinite loop I tried debugging it but I just noticed that at some point (around 80 iterations) the numbers start to be weird a bit like 8100, 4100, 6900, etc..
I'm having hard time tracking it because there's a lot of iterations and for the most part it seems to be doing it right, there must be some issue at one point that causes the numbers to change from what should be and then the result is infinite loop.
The code itself is quite simple:
for(i=0; i<cases; i++)
{
scanf("%d",&original_num);
num_check=original_num;
do
{
power = num_check*num_check;
result_num = (power/100)%10000;
iterations++;
if(num_check==result_num || original_num==result_num)
flag=0;
else
num_check=result_num;
} while(flag);
answerarray[i] = iterations;
iterations=0;
flag=1;
}
Your loop ends when result_num equals the previous number or the original number.
For 5761, you know that the number of iterations is 88. Try printing out those 88 numbers and you should be able to identify why it is looping.
Yeah I printed the output to file and saw the one condition that was missing, thanks :)