forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime_factoriziation.c
164 lines (144 loc) · 3.08 KB
/
prime_factoriziation.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
AUTHOR: Christian Bender
DATE: 12.02.2019
DESCRIPTION: This program calculates the prime factoriziation of a positive
integer > 1
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* initial length of the dynamic array */
#define LEN 10
/* increasing range */
#define STEP 5
/*
this type is for the representation of the prim factoriziation
- its series/range of prime factors
- its length : numbers of prime factors
*/
typedef struct data
{
int *range;
int length;
} range;
typedef range *Range;
/* int_fac : calculates the prime factoriziation of positive integers */
Range int_fact(int);
/* print_arr : prints the integer (heap) array*/
void print_arr(Range);
/* increase : increases the dynamic integer array */
int *increase(int *, int);
/* destroy: destroys the range-structure */
void destroy(Range);
/*
main : simle frame program with a simple UI
*/
int main()
{
int n = 0; /* for user input */
printf("\t\tPrim factoriziation\n\n");
printf("positive integer (> 1) ? ");
scanf("%d", &n);
Range r = int_fact(n);
printf("\nThe factoriziation are: ");
print_arr(r);
destroy(r);
return 0;
}
Range int_fact(int n)
{
assert(n > 1); /* precondition : n must be greater then 1*/
int len = LEN;
int count = 0;
int i = 0;
int *range = (int *)malloc(sizeof(int) * len);
assert(range);
Range pstr = (Range)malloc(sizeof(range));
assert(pstr);
while (n % 2 == 0)
{
n /= 2;
if (i < len)
{
range[i] = 2;
i++;
}
else
{
range = increase(range, len);
len += STEP;
range[i] = 2;
i++;
}
count++;
}
int j = 3;
while (j * j <= n)
{
while (n % j == 0)
{
n /= j;
if (i < len)
{
range[i] = j;
i++;
}
else
{
range = increase(range, len);
len += STEP;
range[i] = j;
i++;
}
count++;
}
j += 2;
}
if (n > 1)
{
if (i < len)
{
range[i] = n;
i++;
}
else
{
range = increase(range, len);
len += STEP;
range[i] = n;
i++;
}
count++;
}
pstr->range = range;
pstr->length = count;
return pstr;
}
void print_arr(Range pStr)
{
assert(pStr); /* checks whether pStr is a null-pointer */
int i = 0;
printf("\n");
for (i; i < pStr->length; i++)
{
if (i == 0)
printf("%d", pStr->range[0]);
else
printf("-%d", pStr->range[i]);
}
printf("\n");
}
int *increase(int *arr, int len)
{
assert(arr); /* checks whether arr is a null-pointer */
int *tmp = (int *)realloc(arr, sizeof(int) * (len + STEP));
assert(tmp);
return tmp;
// assert(arr);
}
void destroy(Range r)
{
free(r->range);
free(r);
}