//Determine the prime factors of a number
for(i = 2; i <= num; i++) { //Loop to check the factors.
while(num % i == 0) { //While the input is divisible to "i" which is initially 2.
printf("%d ", i); //Print the factor.
num = num / i; //Divide the num by "i" which is initially 2 to change the value of num.
}
}
我知道这是使用 for 循环查找数字的素数因子的方式。但我不知道如何将输出整数表示为其素数因子的乘积。例如,INPUT IS:10 | | OUTPUT IS:2 x 5 = 10。我们如何做到这一点?TIA.

你应该:
保存原始值。
打印每个质数因子之间的运算符x
。
最后打印原始值。
#include <stdio.h>
int main(void) {
int num;
int i;
int start_num;
int is_first = 1;
if(scanf("%d", &num) != 1) return 1;
start_num = num; //Save the original value.
//Determine the prime factors of a number
for(i = 2; i <= num; i++) { //Loop to check the factors.
while(num % i == 0) { //While the input is divisible to "i" which is initially 2.
if(!is_first) printf("x "); //Print the operator before second and later operands.
printf("%d ", i); //Print the factor.
num = num / i; //Divide the num by "i" which is initially 2 to change the value of num.
is_first = 0; //Mark that there is already one or more operand.
}
}
printf("= %d\n", start_num); //Print the original value.
return 0;
}

您可以使用适当的标点符号输出因子:
// Output the prime factors of a number
void factorize(int num) {
int n = num; // save the initial value of num
const char *sep = ""; // initial separator is an empty string
for (int i = 2; i <= num / i; i++) { // stop when num is reduced to a prime
while (num % i == 0) { // while the input is divisible to "i"
num = num / i; // divide the num by "i" (remove the factor)
printf("%s%d", sep, i); // print the separator and the factor.
sep = " x "; // change the separator for any further factors
}
}
if (num > 1 || n <= 1) {
printf("%s%d", sep, num); // print the last or single factor.
}
printf(" = %d\n", n); // print the rest of the equation
}
我已经修改了代码,给出了比我之前发布的更健壮的东西,以及稍微更有效。我再次假设你想要通过 stdin 在范围内的(unsigned)32 位输入:[1, 2^32 - 1]
就算法而言,显而易见的是,搜索因子只需要达到floor(sqrt(num))
的测试候选。也有具有多重性的因子,例如(24) => {2, 2, 2, 3}
。
此外,在分解出(2)之后,只需要测试奇数因子。
对于 32 位(无符号)类型,将有少于(32)个素数因子。这给出了用于存储连续素数因子的固定大小数组的简单上限。根据所使用的算法,数组中的素数因子按升序排列。
/******************************************************************************/
#include <stdio.h>
int main (void)
{
/* print a value in [1, 2^32 - 1] as a product of primes: */
unsigned long int n, u, prime[32];
int np = 0;
if (scanf("%lu", & n) != 1 ||
((n == 0) || ((n & 0xffffffffUL) != n)))
{
fprintf(stderr, "factor < u32 = 1 .. 2^32 - 1 >\n");
return (1);
}
if (n == 1) /* trivial case: */
{
fprintf(stdout, "1 = 1\n");
return (0);
}
u = n; /* (u) = working value for (n) */
for (; (u & 0x1) == 0; u >>= 1) /* while (u) even: */
prime[np++] = (2);
while (u > 1)
{
unsigned long q, d = 3, c = 0; /* (c)omposite */
if (np != 0) /* start at previous odd (prime) factor: */
d = (prime[np - 1] == 2) ? (3) : prime[np - 1];
for (; (c == 0) && (q = u / d) >= d; )
{
if ((c = (q * d == u)) == 0) /* not a factor: */
d += 2;
}
prime[np++] = (d = (c == 0) ? u : d);
u /= d; /* if (u) is prime, ((u /= d) == 1) (done) */
}
for (int i = 0; i < np; i++)
{
const char *fmt = (i < np - 1) ? ("%lu x ") : ("%lu = ");
fprintf(stdout, fmt, prime[i]);
}
fprintf(stdout, "%lu\n", n);
return (0);
}
/******************************************************************************/
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(4条)