-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem5.cpp
220 lines (205 loc) · 5.33 KB
/
Problem5.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include<iostream>
using namespace std;
#include<math.h>
void find_smallest_largest_factors(int num_to_factor,
int (&factor_vals)[2])
{
if(num_to_factor%2==0)
{
if(num_to_factor ==2)
{
factor_vals[0]=1;
factor_vals[1]=2;
}
else
{
factor_vals[0]=2;
factor_vals[1]=num_to_factor/2;
}
}
else
{
int composite_test=0;
for (int i=3;i<num_to_factor/2; i+=2)
{
if(num_to_factor%i==0)
{
composite_test=1;
factor_vals[0]=i;
factor_vals[1]=num_to_factor/i;
break;
}
}
if (composite_test == 0)
{
factor_vals[0]=1;
factor_vals[1]=num_to_factor;
}
}
}
int get_number_of_primes_below_num(int input_num)
{
int num_of_primes = 0;
int temp_factor_vals[2] = {0};
for (int i=2; i<=input_num; i++)
{
find_smallest_largest_factors(i,
temp_factor_vals);
if(temp_factor_vals[1]==i)
{
num_of_primes += 1;
}
}
return num_of_primes;
}
int get_arr_of_primes_below_num(
int input_num,
int * &prime_num_val_arr // pass pointer by reference
// int * *prime_num_arr //pass pointer by pointer
)
{
int num_of_primes = get_number_of_primes_below_num(input_num);
prime_num_val_arr = new int [num_of_primes]; //pass by reference
// *prime_num_arr = new int [num_of_primes]; //pass by pointer
int temp_factor_vals[2] = {0};
int x_counter = 0;
for (int i=2; i<=input_num; i++)
{
find_smallest_largest_factors(i,
temp_factor_vals);
if(temp_factor_vals[1]==i)
{
prime_num_val_arr[x_counter] =i;
x_counter += 1;
}
}
// cout << num_of_primes<<x_counter;
return num_of_primes;
}
void add_prime_cnt(
int input_prime_num,
int num_of_primes,
int * &prime_num_val_arr,
int * &prime_num_cnttemp_arr
)
{
for(int i=0; i<num_of_primes; i++)
{
if (prime_num_val_arr[i]==input_prime_num)
{
prime_num_cnttemp_arr[i] += 1;
break;
}
}
}
void get_prime_factors(
int input_num,
int num_of_primes,
int * &prime_num_val_arr,
int * &prime_num_cnttemp_arr
)
{
int prime_factors = 0;
int temp_factor_vals[2] = {0};
int num_to_factor = input_num;
while (prime_factors !=1)
{
find_smallest_largest_factors(num_to_factor,
temp_factor_vals);
if(temp_factor_vals[1]==num_to_factor)
{
add_prime_cnt(
temp_factor_vals[1],
num_of_primes,
prime_num_val_arr,
prime_num_cnttemp_arr);
prime_factors = 1;
}
else
{
add_prime_cnt(
temp_factor_vals[0],
num_of_primes,
prime_num_val_arr,
prime_num_cnttemp_arr);
num_to_factor = temp_factor_vals[1];
}
}
}
void get_max_prime_cnt(
int num_of_primes,
int * &prime_num_cnttemp_arr,
int * &prime_num_cntmax_arr
)
{
for(int i=0; i<num_of_primes; i++)
{
if(prime_num_cntmax_arr[i]<prime_num_cnttemp_arr[i])
{
prime_num_cntmax_arr[i]=prime_num_cnttemp_arr[i];
}
}
}
int get_min_divisble_num(
int num_of_primes,
int * &prime_num_val_arr,
int * &prime_num_cntmax_arr
)
{
int min_divisible_num=1;
for(int i=0; i<num_of_primes; i++)
{
min_divisible_num *= pow(prime_num_val_arr[i],
prime_num_cntmax_arr[i]);
}
return min_divisible_num;
}
void get_max_cnt_of_primes_below_num(
int input_num,
int num_of_primes,
int * &prime_num_val_arr,
int * &prime_num_cntmax_arr
)
{
int *prime_num_cnttemp_arr = new int[num_of_primes];
for (int i=2; i<=input_num; i++)
{
std::fill_n(prime_num_cnttemp_arr, num_of_primes, 0);
get_prime_factors(
i,
num_of_primes,
prime_num_val_arr,
prime_num_cnttemp_arr
);
get_max_prime_cnt(
num_of_primes,
prime_num_cnttemp_arr,
prime_num_cntmax_arr
);
}
}
int main()
{
int input_num, num_of_primes;
int *prime_num_val_arr, *prime_num_cntmax_arr;
int min_divisible_num;
cout << "Provide input integer: ";
cin >>input_num;
num_of_primes = get_arr_of_primes_below_num(
input_num,
prime_num_val_arr);
prime_num_cntmax_arr = new int[num_of_primes];
std::fill_n(prime_num_cntmax_arr, num_of_primes, 1);
get_max_cnt_of_primes_below_num(
input_num,
num_of_primes,
prime_num_val_arr,
prime_num_cntmax_arr);
min_divisible_num = get_min_divisble_num(
num_of_primes,
prime_num_val_arr,
prime_num_cntmax_arr
);
cout << "Min Divisble Num: "<< min_divisible_num << "\n";
return 0;
}