-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime_factor.cpp
More file actions
91 lines (75 loc) · 2.53 KB
/
prime_factor.cpp
File metadata and controls
91 lines (75 loc) · 2.53 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
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <array>
#include <vector>
#include <cmath>
struct Node {
double value;
//Node* parent;
std::vector<Node*> children;
std::vector<int> num;
};
double divBy(long val, int by) {
return val / by;
return 0;
}
bool isPrime(long val) {
if(val < 2) return false;
for(long i = 2; i * i <= val; ++i) {
if(val % i == 0) return false;
}
return true;
}
bool createNeighbors(Node* current) {
current->children.push_back(new Node{divBy(current->value, 2), {}});
current->children.push_back(new Node{divBy(current->value, 3), {}});
current->children.push_back(new Node{divBy(current->value, 5), {}});
current->children.push_back(new Node{divBy(current->value, 7), {}});
return true;
}
bool popStack(std::vector<Node*> stack) {
try {
if(stack.empty()) throw std::runtime_error("stack is empty, cannot pop");
stack.pop_back();
} catch(const std::runtime_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
return false;
}
return true;
}
bool pushStack(std::vector<Node*>& stack, Node* node) {
stack.push_back(node);
return true;
}
int main() {
//long max_product = 600851475143;
double product = pow(2, 8);
std::vector<Node*> nodesToVisitStack;
// create depth-first through tree with stack
Node* root = new Node{product, {}};
int v;
Node* current = nullptr;
pushStack(nodesToVisitStack, root);
while(nodesToVisitStack.size() > 0) {
current = nodesToVisitStack.back();
createNeighbors(current);
//if(current->children[0])
//push(nodesToVisitStack, current);
v = divBy(current->value, 2); // 0 should terminate this branch
std::cout << "divBy result: " << v << std::endl;
current->children[0]->value = v;
if(v == 1) popStack(nodesToVisitStack);
if(v > 0) pushStack(nodesToVisitStack, current->children[0]);
if(nodesToVisitStack.empty()) {
std::cout << "stack empty, terminating" << std::endl;
break;
}
current = nodesToVisitStack.back();
//int childIndex = 0;
//current = current->children.empty() ? nullptr : current->children[0];
//current->children.push_back(new Node{divBy(current->value, 2), {}});
if(current != nullptr) {
std::cout << "current value:" << current->value << std::endl;
}
}
return 0;
}