-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenges.cpp
More file actions
78 lines (63 loc) · 1.57 KB
/
Copy pathchallenges.cpp
File metadata and controls
78 lines (63 loc) · 1.57 KB
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
#include <math.h>
#include <cmath>
#include <iostream>
#include "challenges.h"
BasicFunctions::BasicFunctions(int n) {
this->length = this->getFactorizationCount(n);
this->factors = this->primeFactors(n);
this->originalNumber = n;
}
BasicFunctions::~BasicFunctions() {
delete &this->length;
delete this->factors;
}
int BasicFunctions::getFactorizationCount(int n) {
int array_cnt = 0;
// Adds to all the 2's that divide into n
while (n % 2 == 0) {
array_cnt++;
n /= 2;
}
for (int i = 3; i <= (sqrt(n)); i+= 2) {
while (n % i == 0) {
array_cnt++;
n /= i;
}
}
if (n > 2)
array_cnt++;
return array_cnt;
}
int * BasicFunctions::primeFactors(int n) {
// Get the array size
int array_size = getFactorizationCount(n);
static int *factors;
factors = new int[array_size];
int array_index = 0;
// Adds to all the 2's that divide into n
while (n % 2 == 0) {
factors[array_index] = 2;
array_index++;
n /= 2;
}
for (int i = 3; i <= (sqrt(n)); i+= 2) {
while (n % i == 0) {
factors[array_index] = i;
array_index++;
n /= i;
}
}
if (n > 2) {
factors[array_index] = n;
array_index++;
}
return factors;
}
void BasicFunctions::printIntPointer(int *n, int length) {
for (int i = 0; i <= length; i++) {
std::cout << "Value: " << *(n + i) << std::endl;
}
}
int BasicFunctions::getOriginalNumber() {
return this->originalNumber;
}