-
Notifications
You must be signed in to change notification settings - Fork 5
/
factors.c
53 lines (42 loc) · 907 Bytes
/
factors.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
typedef struct FACTOR
{
int *factors;
int count;
} Factor;
Factor get_factors(int n)
{
int root = (int) sqrt(n);
int *factors = (int *) malloc((n / 2) * sizeof(int));
int j = 0;
for (int i = 1; i <= root; i++) {
if (n % i == 0) {
factors[j++] = i;
if (i != (n / i)) {
factors[j++] = n / i;
}
}
}
Factor factor = {
.factors = factors,
.count = j
};
return factor;
}
int main()
{
int n;
Factor factors;
printf("%s", "Enter n: ");
scanf("%d", &n);
factors = get_factors(n);
printf("Total factors: %d\n", factors.count);
for (int i = 0; i < factors.count; i++) {
printf("%d ", factors.factors[i]);
assert((n % factors.factors[i]) == 0);
}
return 0;
}