patterncMinor
Finding the prime factors of a positive number
Viewed 0 times
factorsthenumberfindingpositiveprime
Problem
I have an assignment to write on the standard output the prime factors of a positive number displayed in ascending order and separated by '*'. My code works, but I need help reviewing it before I submit my work. Allowed functions:
write, printf, atoi.void find_factors(int n)
{
int i;
int separated;
i = 3;
separated = 0;
if (n 2)
{
if (separated)
printf("*");
printf("%d", n);
}
}Solution
1) The reasons for 2 loops
while (n % 2 == 0) and while (i qu, code is done.Code Snippets
int i = 2;
while (i <= n) {
while(n % i == 0) {
...
printf("%d", i);
separated = 1;
n = n / i;
}
i += i==2 ? 1 : 2;
}char *separator = "";
...
while (...) {
printf("%s%d", separator, 2);
separator = "*";
...
}find_factors(INT_MAX); // infinite loop
find_factors(1); // no output
find_factors(0); // infinite loop
find_factors(negative) // no output// int i;
// int separated;
// i = 3;
// separated = 0;
int i = 3;
int separated = 0;#include <stdbool.h>
// int separated = 0;
bool separated = false;Context
StackExchange Code Review Q#132013, answer score: 4
Revisions (0)
No revisions yet.