C素因子分解(循环失败?)

Ben*_*Ben 0 c for-loop

我一直在研究这段简单的代码1.5小时,但没有发现错误.我开始疯了;)

你们这些有着清新头脑和观点的人能给我一点暗示,我可能会犯错吗?(我对C比较新)

问题是:代码适用于我输入和测试的大多数数字,但我偶然发现了一个不起作用的数字:3486118(或55777888,它是它的倍数)它适用于第一个循环但是在因子2之后它变成了无限循环.

这是我的代码:(非常感谢任何帮助)

// Program calculates prime factors of entered number and returns them

#include <stdio.h>

int main() {

    long int num, num_cp;
    long int product=1;

    /*prime number array up to 100.000*/
    long int prime[] = {2, 3, **[...cut out MANY numbers...]** 99971, 99989, 99991};

    printf("Please enter a positive integer:\n");
    scanf("%li", &num);//55777888 or 3486118 not working... why?
    //copy the entered number to keep the original for comparison with "product" and "break;" if equal
    num_cp=num;

    printf("prime factorization of %li:\n\n", num);

    for (int i=0; i<sizeof(prime); i++) {
        if (num_cp%prime[i]==0) {
            num_cp/=prime[i];
            product*=prime[i];
            if (product==num) {
                printf("%li\n\n", prime[i]);
                break;
            }
            printf("%li*", prime[i]);

            //If prime factor found but "product" is still not equal to "num" reset loop counter "i" to -1 (==0 in next loop)
            i=-1;
        }
    }
    printf("END");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

ros*_*sum 5

"我现在一直在研究这段简单的代码1.5小时,但没有发现错误.我开始发疯了;)"

别.别管它.走开去吃披萨.在你最喜欢的电影面前出来.洗个澡.瞄准2048年(或其他)的新高分.你的大脑卡在车辙中,你不再看到你的代码了.您只看到您认为您的代码是什么.

当你的大脑离开车辙时,那么 - 只有这样 - 回去并实际阅读你写的代码.不是您认为编写的代码,而是您实际编写的代码.是的,他们是不同的.