[英]I just am not sure where I am going wrong. Writing a program to print out prime factorization in c and practicing linking along with it
In this program I keep getting a floating point exception at the end. 在这个程序中,我总是在最后得到一个浮点异常。 There are two main files I am working with.
我正在使用两个主要文件。 The first is the "main" listed below:
第一个是下面列出的“主要”:
int main(){
int ans;
do{
printf("Enter an integer greater than 1:\n");
scanf("%d", &ans);
}while(ans <= 1);
printf("%d = ", ans);
int d = 2;
while(ans >= d){
if(ans == d){
printf("%d ^ %d", d, factor_power(ans, d));
ans = ans / (d ^ (factor_power(ans, d)));
}
else{
printf("%d ^ %d * ", d , factor_power(ans , d));
ans = ans/(d ^ (factor_power(ans, d)));
d++;
}
}
printf("\n");
return 0;
}
The file that contains the factor_power() method is here: 包含factor_power()方法的文件在这里:
int factor_power(int n, int d){
int p = 1;
do{
if( n % (d ^ p) == 0)
p ++;
}while(n % (d^(p+1)) == 0);
return p;
}
both include my header, math.h, and stdio.h. 都包括我的头文件math.h和stdio.h。 I am just so lost on where the floating point exception is coming from.
我对浮点异常来自何处感到迷茫。 The program is supposed to print out the prime factors like:
该程序应该打印出主要因素,例如:
1200 = 2^4 * 3^1 * 5*2. 1200 = 2 ^ 4 * 3 ^ 1 * 5 * 2。
Amy feedback is much appreciated. 艾米的反馈意见非常感谢。
Try following: 请尝试以下操作:
int ipow(int base, int exp)
{
int power = 1;
while (exp)
{
if (exp & 1)
power *= base;
exp >>= 1;
base *= base;
}
return power;
}
int factor_power(int n, int d){
int p = 1;
do{
if( (n % ipow(d,p)) == 0)
p++;
} while((n % ipow(d,(p+1))) == 0);
return p;
}
You may have to include "math.h" if u already hadn't. 如果您尚未添加“ math.h”,则可能必须包含。
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.