# Help me resolve this issue

this is a program used for : - given (n=6) it finds smallest divisor(i.e - 2) and then adds to it(i.e - 6+2 = 8) thus n=8 .This process is repeated for k times… output should be final value of n…The codechef ide throws SIGTSTP error.Please help me resolve this issue. Also, what can I do in future to avoid such incidents ?

int main()
{
int t;
std::cin >> t;

for (int i=0 ; i< t; ++i)
{
int n ,k;
std::cin >> n >> k;
for (int i=0 ; i<k;++i)
{
for (int i=2 ; i<=n ;++i)
{
if (n%i == 0)
{
n = n+i;
}

``````            }
std::cout << n << '\n';
}
}
return 0;
``````

}

Your mistake is when you have added i to n, then also your inner loop is running. Also, both loop have same variable.

``````          #include <iostream>

int  main()
{
int t;
std::cin >> t;

for (int j=0 ; j< t; ++j)
{
int n ,k;
std::cin >> n >> k;
for (int w=0 ; w<k;++w)
{
for (int i=2 ; i<=n ;++i)
{
if (n%i == 0)
{
n = n+i;
}

}

}
std::cout << n << '\n';
}
return 0;
``````

}

I have doubt , why is n=n+i not correct ?

Try dry run for n=3 and k=2 What should be output and what your code is giving. Also, I think this can be done O(sqrt(n)) (only you have to find first factor)

This is so naive, but if you want to just try out this way, Break the loop once you meet the if condition