在從@neal AISE here to get prime factors答案: 我所做的:問題主要算法用C
/*neal aise's code*/
printPrimeFactors(int num) {
int i;
for (i = 2; i < sqrt(num); i=next_prime(i)) {
if (num %i){
printf("%d", i);
}
}
}
/*my code*/
int next_prime(int p){
int prime_found = 0;
while (!prime_found){
if (p <= 1)/* if low number comes in, then */
p = 2; /* next prime is always 2 (first prime) */
else
if ((p % 2) == 0) /* no even primes */
p++; /* make the number odd before test */
else
p += 2; /* get next odd numero to test */
prime_found = is_prime(p); /*check if number is prime*/
}
return (p);
}
int is_prime(int p){
int curr_num = 2; /* start divisor at 2 */
float stop_num = sqrt((float) p); /* no divisor > sqrt of number needed */
while(curr_num <= stop_num){
if ((p % curr_num) == 0) /* not prime if evenly divisible */
return (0);
else
curr_num++; /* increment divisor */
}
return(1); /* not evenly divisible, return prime */
}
如何moddify的代碼功能
printPrimeFactors()
所以它按需運作?
嘗試在代碼中添加一些'cout <<'語句。他們總是幫助我調試這樣的事情。 – Blender
你的'is_prime()'函數做了什麼? – bdares
你可以發佈你的'is_prime()'代碼嗎?此外,輸入/輸出代碼... – Jon