-
Notifications
You must be signed in to change notification settings - Fork 0
/
prob12_functs.c
115 lines (87 loc) · 2.38 KB
/
prob12_functs.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
/*
* prob12_functs.c
*
* Created on: Aug 12, 2012
* Author: ssimmons
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "myfuncts.h"
static void initialize_factor_list(int current_length, factor *factors);
void *era_sieve(int input[], int length){
// More or less adapted from problem 7, but with pointer malloc'd rather than having mostly
// empty array.
int *primes = NULL;
int i=0, j=0, counter=0;
input[0] = 0;
input[1] = 0;
for(i = 2; i<length; i++){ // Set all of input array to 1
input[i] = 1;
}
for (i = 2; i< ( (int) ceil(sqrt(length)) ); i++){ // Do actual sieving
if(input[i] == 1){
for(j=(i*i); j<length; j += i){
input[j] = 0;
counter++;
}
}
}
primes = (int *) malloc((counter+1)*sizeof(int));
i = 0, j=0;
for(i = 2; i<length; i++){
if(input[i] == 1){
*(primes+j) = i;
j++;
}
}
// Now we've set primes to contain all of the prime numbers sieved earlier
*(primes+j) = 0;
return primes;
}
void *factorize(int input, int *primes){
//Use trial divisions to put factors of input as list using primes[]
//For prob 12, will have max of ~1000 factors as primes will be of length ~1000
int i=0, counter=0;
int current_size = MIN_FACTOR_SIZE;
factor *factors = (factor *) malloc(sizeof(factor)*(MIN_FACTOR_SIZE));
factors->exponent = 0;
factors->value = 1;
initialize_factor_list(current_size, factors);
while(*(primes+i) != 0){
while( input % *(primes+i) == 0){
counter++;
input /= *(primes+i);
}
if((factors+i)->exponent != -1){
(factors+i)->value = *(primes+i);
(factors+i)->exponent = counter;
}
else{
current_size += MIN_FACTOR_SIZE;
factors = realloc(factors, (sizeof(factor)*current_size));
initialize_factor_list(MIN_FACTOR_SIZE, (factors+i));
(factors+i)->value = *(primes+i);
(factors+i)->exponent = counter;
}
counter = 0;
i++;
}
return factors;
}
static void initialize_factor_list(int current_length, factor *factors){
int i;
for(i = 1; i< (current_length-1); i++){
(factors+i)->value = 1;
(factors+i)->exponent = 0;
}
(factors+(current_length-1))->value = 1;
(factors+(current_length-1))->exponent = -1;
// Initialize pointer to dynamic factor array:
// By default, factor value == 1 and exponent set to 0,
// except for last value in array, which is set to -1.
}
void clean(int *primes, factor *factors){
free(primes);
free(factors);
}