-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallengeFive.cpp
More file actions
55 lines (44 loc) · 1.18 KB
/
Copy pathChallengeFive.cpp
File metadata and controls
55 lines (44 loc) · 1.18 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
// Project Euler Challenge Five
#include "ChallengeFive.h"
#include <iostream>
ChallengeFive::ChallengeFive(int number) {
///TODO: Allow input of a variable argument
this->factorable = number;
}
ChallengeFive::~ChallengeFive() {
///TODO: Delete Local Variables to conserve memory
delete &this->factorable;
}
int primesBefore(int n) {
int total = 0;
for (int i = 2; i <= n; i++) {
BasicFunctions bf(i);
if (bf.length == 1) total++;
}
return total;
}
// Do a pass by reference so variables are directly modified (it's also faster)
void compare_factors(int *a, int b) {
// Generate the list of primes < a and < b
int gcd = 0;
// YAY!!! COMMA OPERATOR!!!
for (int i = 1; i < (*a > b ? *a : b); i++) {
if (*a % i == 0 && b % i == 0) {
gcd = i;
}
}
// A or B is prime
if (gcd == 0) {
*a *= b;
} else {
*a = (*a * b) / gcd;
}
}
void ChallengeFive::run() {
int product = 1;
// Start Prime Factorization At Two
for (int i = 1; i < this->factorable; i++) {
compare_factors(&product, i);
}
std::cout << std::endl << "Product: " << product << std::endl;
}