For some reason this cycle doesn't stop, without the else it doesn't even print the first line and I dont't know how to fix it
for(int i = 0; prime <= sqrt(rest); ++i) {
printf("%d", i);
if (primes[i] == 1) {
prime = primes[i];
printf("%d", prime);
while (rest % prime == 0) {
rest = rest / prime;
rep++;
}
if (rep == 1) {
printf("%d", i);
print++;
} else if (rep > 1) {
printf("%d^%d", prime, rep);
print++;
}
if (rest != 1 && print != 0) {
printf(" x ");
} else if (print != 0) {
printf("\n");
}
rep = 0;
print = 0;
}else{
printf("WTF\n%d\n", i);
i++;
}
}
if (rest > 1) {
printf("%lld\n", rest);
} else{
printf("WTF\n");
break;
}
}
最佳答案
你永远不会改变素数的值,这就是你的条件被设置为停止的原因,所以如果它永远不会改变,它就永远不会停止。也许你的意思是在 for 循环开始时是 prime 而不是 i ?
关于c - for循环一定有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47191606/