diff --git a/.gitignore b/.gitignore index 32d1ca3..a0dc815 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ python/__pycache__ rust/*/Cargo.lock rust/*/target/* cpp/a.out +cpp/*/*.o java/*/*.class scheme/*.so diff --git a/cpp/append_digit_primes.cpp b/cpp/append_digit_primes.cpp deleted file mode 100644 index b47e037..0000000 --- a/cpp/append_digit_primes.cpp +++ /dev/null @@ -1,107 +0,0 @@ -#include -#include -#include -#include - -using ull = unsigned long long; - -auto next(ull x) -> std::vector; -auto is_prime(ull n) -> bool; - -class Tree -{ -public: - ull value; - std::vector children; - - Tree(ull value, std::vector tree_children) : value(value) { - for (ull child : tree_children) { - children.push_back(Tree(child, {})); - } - } - - void step() { - if (children.size() == 0) { - auto xs = next(value); - for (auto val : xs) { - children.push_back(Tree(val, {})); - } - } else { - for (auto &child : children) { - child.step(); - } - } - } - - auto longest_path() -> std::vector { - if (children.size() == 0) { - return { value }; - } - std::vector max_path; - int max_length = 0; - for (auto &child : children) { - auto temp = child.longest_path(); - if (temp.size() > max_length) { - max_length = temp.size(); - max_path = temp; - } - } - std::vector retval = { value }; - retval.insert(retval.end(), max_path.begin(), max_path.end()); - return retval; - } -}; - -auto next(ull x) -> std::vector { - std::vector new_xs; - ull temp = x * 10; - for (int i = 0; i < 10; i++) { - auto temp2 = temp + i; - if (is_prime(temp2)) { - new_xs.push_back(temp2); - } - } - return new_xs; -} - -auto is_prime(ull n) -> bool { - static std::unordered_map primes; - if (n < 2) { - primes[n] = false; - return false; - } - if (primes.count(n) > 0) { - return primes[n]; - } - for (int i = 2; i * i <= n; i++) { - if (n % i == 0) { - primes[n] = false; - return false; - } - } - primes[n] = true; - return true; -} - -template -auto print_vec(std::vector vec) -> void { - std::cout << '['; - for (T &val : vec) { - std::cout << val << ", "; - } - std::cout << "]\n"; -} - -auto main() -> int { - /* std::vector primes; */ - /* for (int i = 2; i < 10000000; i++) { */ - /* if (is_prime(i)) { */ - /* primes.push_back(i); */ - /* } */ - /* } */ - Tree tree(0, {}); - for (int i = 0; i < 9; i++) { - tree.step(); - print_vec(tree.longest_path()); - } -} diff --git a/cpp/append_digit_primes/Makefile b/cpp/append_digit_primes/Makefile new file mode 100644 index 0000000..e2cddb8 --- /dev/null +++ b/cpp/append_digit_primes/Makefile @@ -0,0 +1,23 @@ +# rename `template` to name of project +TARGET=append_digit_primes +CXX=g++ +OFILES=../common/tree.o ../common/util.o +LIBS= +CXXFLAGS=-g + +$(TARGET): $(OFILES) $(TARGET).o + $(CXX) $(CXXFLAGS) -o $@ $(TARGET).o $(OFILES) $(LIBS) + +%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +PHONY: clean run all + +clean: + rm *.o $(TARGET) + +run: + ./$(TARGET) + +all: + make $(TARGET) diff --git a/cpp/append_digit_primes/append_digit_primes.cpp b/cpp/append_digit_primes/append_digit_primes.cpp new file mode 100644 index 0000000..08600c6 --- /dev/null +++ b/cpp/append_digit_primes/append_digit_primes.cpp @@ -0,0 +1,28 @@ +#include "../common/util.h" +#include "../common/tree.h" + +#include + +auto next(ull x) -> std::vector { + std::vector new_xs; + ull temp = x * 10; + for (int i = 0; i < 10; i++) { + auto temp2 = temp + i; + if (is_prime(temp2)) { + new_xs.push_back(temp2); + } + } + return new_xs; +} + +auto main(int argc, char *argv[]) -> int { + if (argc != 2) { + std::cout << "Usage: append_digit_primes \n"; + return -1; + } + Tree tree(0, {}); + for (int i = 0; i < std::atoi(argv[1]); i++) { + tree.step(next); + print_vec(tree.longest_path()); + } +} diff --git a/cpp/common/tree.cpp b/cpp/common/tree.cpp new file mode 100644 index 0000000..589addc --- /dev/null +++ b/cpp/common/tree.cpp @@ -0,0 +1,41 @@ +#include "tree.h" + +/* all tree stuff, don't really need to change it unless you have some ideas for + * optimizations + */ +Tree::Tree(ull value, std::vector tree_children) : value(value) { + for (ull child : tree_children) { + children.push_back(Tree(child, {})); + } +} + +void Tree::step(std::vector (*next)(ull)) { + if (children.size() == 0) { + auto xs = next(value); + for (auto val : xs) { + children.push_back(Tree(val, {})); + } + } else { + for (auto &child : children) { + child.step(next); + } + } +} + +auto Tree::longest_path() -> std::vector { + if (children.size() == 0) { + return { value }; + } + std::vector max_path; + int max_length = 0; + for (auto &child : children) { + auto temp = child.longest_path(); + if (temp.size() > max_length) { + max_length = temp.size(); + max_path = temp; + } + } + std::vector retval = { value }; + retval.insert(retval.end(), max_path.begin(), max_path.end()); + return retval; +} diff --git a/cpp/common/tree.h b/cpp/common/tree.h new file mode 100644 index 0000000..7f88c26 --- /dev/null +++ b/cpp/common/tree.h @@ -0,0 +1,17 @@ +#include + +using ull = unsigned long long; + +/* all tree stuff, don't really need to change it unless you have some ideas for + * optimizations + */ +class Tree +{ +public: + ull value; + std::vector children; + + Tree(ull value, std::vector tree_children); + void step(std::vector (*next)(ull)); + auto longest_path() -> std::vector; +}; diff --git a/cpp/common/util.cpp b/cpp/common/util.cpp new file mode 100644 index 0000000..54b4e85 --- /dev/null +++ b/cpp/common/util.cpp @@ -0,0 +1,30 @@ +#include "util.h" + +/* some helper functions */ +auto is_prime(ull n) -> bool { + static std::unordered_map primes; + if (n < 2) { + primes[n] = false; + return false; + } + if (primes.count(n) > 0) { + return primes[n]; + } + for (int i = 2; i * i <= n; i+=2) { + if (n % i == 0) { + primes[n] = false; + return false; + } + if (i == 2) i--; + } + primes[n] = true; + return true; +} + +auto print_vec(std::vector vec) -> void { + std::cout << '['; + for (ull val : vec) { + std::cout << val << ", "; + } + std::cout << "]\n"; +} diff --git a/cpp/common/util.h b/cpp/common/util.h new file mode 100644 index 0000000..49b1d86 --- /dev/null +++ b/cpp/common/util.h @@ -0,0 +1,10 @@ +#include +#include +#include + +using ull = unsigned long long; + +/* some helper functions */ +auto is_prime(ull n) -> bool; + +auto print_vec(std::vector vec) -> void; diff --git a/cpp/template.cpp b/cpp/template.cpp deleted file mode 100644 index 9793c62..0000000 --- a/cpp/template.cpp +++ /dev/null @@ -1,104 +0,0 @@ -#include -#include -#include -#include - -using ull = unsigned long long; - -auto next(ull x) -> std::vector; -auto is_prime(ull n) -> bool; - -/* all tree stuff, don't really need to change it unless you have some ideas for - * optimizations - */ -class Tree -{ -public: - ull value; - std::vector children; - - Tree(ull value, std::vector tree_children) : value(value) { - for (ull child : tree_children) { - children.push_back(Tree(child, {})); - } - } - - void step() { - if (children.size() == 0) { - auto xs = next(value); - for (auto val : xs) { - children.push_back(Tree(val, {})); - } - } else { - for (auto &child : children) { - child.step(); - } - } - } - - auto longest_path() -> std::vector { - if (children.size() == 0) { - return { value }; - } - std::vector max_path; - int max_length = 0; - for (auto &child : children) { - auto temp = child.longest_path(); - if (temp.size() > max_length) { - max_length = temp.size(); - max_path = temp; - } - } - std::vector retval = { value }; - retval.insert(retval.end(), max_path.begin(), max_path.end()); - return retval; - } -}; - -/* TODO Tweak this to change functionality according to explanation on the - * Overleaf - */ -auto next(ull x) -> std::vector { - std::vector new_xs; - return new_xs; -} - -/* some helper functions */ -auto is_prime(ull n) -> bool { - static std::unordered_map primes; - if (n < 2) { - primes[n] = false; - return false; - } - if (primes.count(n) > 0) { - return primes[n]; - } - for (int i = 2; i * i <= n; i++) { - if (n % i == 0) { - primes[n] = false; - return false; - } - } - primes[n] = true; - return true; -} - -template -auto print_vec(std::vector vec) -> void { - std::cout << '['; - for (T &val : vec) { - std::cout << val << ", "; - } - std::cout << "]\n"; -} - -auto main() -> int { - /* starts off with a tree with value 0 and no children and searches 20 - * iterations, feel free to tweak to whatever you want - */ - Tree tree(0, {}); - for (int i = 0; i < 20; i++) { - tree.step(); - print_vec(tree.longest_path()); - } -} diff --git a/cpp/template/Makefile b/cpp/template/Makefile new file mode 100644 index 0000000..9456565 --- /dev/null +++ b/cpp/template/Makefile @@ -0,0 +1,23 @@ +# rename `template` to name of project +TARGET=template +CXX=g++ +OFILES=../common/tree.o ../common/util.o +LIBS= +CXXFLAGS=-g + +$(TARGET): $(OFILES) $(TARGET).o + $(CXX) $(CXXFLAGS) -o $@ $(TARGET).o $(OFILES) $(LIBS) + +%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +PHONY: clean run all + +clean: + rm *.o $(TARGET) + +run: + ./$(TARGET) + +all: + make $(TARGET) diff --git a/cpp/template/template.cpp b/cpp/template/template.cpp new file mode 100644 index 0000000..f0012e4 --- /dev/null +++ b/cpp/template/template.cpp @@ -0,0 +1,19 @@ +/* include little library I made */ +#include "../common/util.h" +#include "../common/tree.h" + +auto next(ull x) -> std::vector { + std::vector new_xs; + return new_xs; +} + +auto main() -> int { + /* starts off with a tree with value 0 and no children and searches 20 + * iterations, feel free to tweak to whatever you want + */ + Tree tree(0, {}); + for (int i = 0; i < 20; i++) { + tree.step(next); + print_vec(tree.longest_path()); + } +} diff --git a/python/append_digit_primes.py b/python/append_digit_primes.py index 5add74d..4c85055 100644 --- a/python/append_digit_primes.py +++ b/python/append_digit_primes.py @@ -1,76 +1,21 @@ import math -class Tree: - def __init__(self, value, children): - self.value = value - self.children = [] - for child in children: - self.children.append(Tree(child, [])) +from tree import Tree +import util - def step(self): - if len(self.children) == 0: - for val in step(self.value): - self.children.append(Tree(val, [])) - return - for child in self.children: - child.step() - - def __str__(self): - retval = '[' + str(self.value) + ': ' - for child in self.children: - retval += str(child) - retval += ']' - return retval - - def __repr__(self): - return str(self) - - def longest_path(self): - if len(self.children) == 0: - return [self.value] - max_path = [] - max_length = 0 - for child in self.children: - temp = child.longest_path() - if len(temp) > max_length: - max_path = temp - max_length = len(temp) - return [self.value] + max_path - -def is_square(x): - return int(math.sqrt(x)) == math.sqrt(x) - -primes = {} - -def is_prime(x): - try: - return primes[x] - except KeyError: - if x < 2: - primes[x] = False - return False - i = 2 - while i * i <= x: - if x % i == 0: - primes[x] = False - return False - i += 1 - primes[x] = True - return True - -def step(x): +def next(x): new_xs = [] for i in range(10): - if is_prime(10 * x + i): + if util.is_prime(10 * x + i): new_xs.append(10 * x + i) return new_xs def main(): tree = Tree(0, []) - for i in range(9): + for i in range(20): print(tree.longest_path()) - tree.step() + tree.step(next) print(tree.longest_path()) diff --git a/python/insert_digit_anywhere_primes.py b/python/insert_digit_anywhere_primes.py index 7cf8cf3..c96b53e 100644 --- a/python/insert_digit_anywhere_primes.py +++ b/python/insert_digit_anywhere_primes.py @@ -1,68 +1,16 @@ import math -class Tree: - def __init__(self, value, children): - self.value = value - self.children = [] - for child in children: - self.children.append(Tree(child, [])) +from tree import Tree +import util - def step(self): - if len(self.children) == 0: - for val in step(self.value): - self.children.append(Tree(val, [])) - return - for child in self.children: - child.step() - - def __str__(self): - retval = '[' + str(self.value) + ': ' - for child in self.children: - retval += str(child) - retval += ']' - return retval - - def __repr__(self): - return str(self) - - def longest_path(self): - if len(self.children) == 0: - return [self.value] - max_path = [] - max_length = 0 - for child in self.children: - temp = child.longest_path() - if len(temp) > max_length: - max_path = temp - max_length = len(temp) - return [self.value] + max_path - -def is_square(x): - return int(math.sqrt(x)) == math.sqrt(x) - -primes = {} - -def is_prime(x): - try: - return primes[x] - except KeyError: - i = 2 - while i * i <= x: - if x % i == 0: - primes[x] = False - return False - i += 1 - primes[x] = True - return True - -def step(x): +def next(x): new_xs = [] str_x = str(x) for (i,digit) in enumerate(str_x + '0'): for d in range(0,10): temp = str_x[:i] + str(d) + str_x[i:] if not (i == 0 and d == 0): - if is_prime(int(temp)): + if util.is_prime(int(temp)): new_xs.append(int(temp)) return new_xs @@ -71,7 +19,7 @@ def main(): tree = Tree(0, [2, 3, 5, 7]) for i in range(5): print(tree.longest_path()) - tree.step() + tree.step(next) print(tree.longest_path()) print(tree) diff --git a/python/insert_digit_anywhere_squares.py b/python/insert_digit_anywhere_squares.py index 9b181cf..941dab1 100644 --- a/python/insert_digit_anywhere_squares.py +++ b/python/insert_digit_anywhere_squares.py @@ -1,53 +1,16 @@ import math -class Tree: - def __init__(self, value, children): - self.value = value - self.children = [] - for child in children: - self.children.append(Tree(child, [])) +from tree import Tree +import util - def step(self): - if len(self.children) == 0: - for val in step(self.value): - self.children.append(Tree(val, [])) - return - for child in self.children: - child.step() - - def __str__(self): - retval = '[' + str(self.value) + ': ' - for child in self.children: - retval += str(child) - retval += ']' - return retval - - def __repr__(self): - return str(self) - - def longest_path(self): - if len(self.children) == 0: - return [self.value] - max_path = [] - max_length = 0 - for child in self.children: - temp = child.longest_path() - if len(temp) > max_length: - max_path = temp - max_length = len(temp) - return [self.value] + max_path - -def is_square(x): - return int(math.sqrt(x)) == math.sqrt(x) - -def step(x): +def next(x): new_xs = [] str_x = str(x) for (i,digit) in enumerate(str_x + '0'): for d in range(0,10): temp = str_x[:i] + str(d) + str_x[i:] if not (i == 0 and d == 0): - if is_square(int(temp)): + if util.is_square(int(temp)): new_xs.append(int(temp)) return new_xs @@ -55,7 +18,7 @@ def step(x): def main(): tree = Tree(0, [1,4,9]) for i in range(20): - tree.step() + tree.step(next) print(tree) print(tree.longest_path()) diff --git a/python/template.py b/python/template.py index 91a54e5..d740478 100644 --- a/python/template.py +++ b/python/template.py @@ -1,65 +1,8 @@ import math -# tree stuff, I wouldn't recommend tweaking unless you have serious ideas for -# optimizations -class Tree: - def __init__(self, value, children): - self.value = value - self.children = [] - for child in children: - self.children.append(Tree(child, [])) - - def step(self): - if len(self.children) == 0: - for val in next(self.value): - self.children.append(Tree(val, [])) - return - for child in self.children: - child.step() - - def __str__(self): - retval = '[' + str(self.value) + ': ' - for child in self.children: - retval += str(child) - retval += ']' - return retval - - def __repr__(self): - return str(self) - - def longest_path(self): - if len(self.children) == 0: - return [self.value] - max_path = [] - max_length = 0 - for child in self.children: - temp = child.longest_path() - if len(temp) > max_length: - max_path = temp - max_length = len(temp) - return [self.value] + max_path - -# some utility functions, same as above -def is_square(x): - return int(math.sqrt(x)) == math.sqrt(x) - -primes = {} - -def is_prime(x): - try: - return primes[x] - except KeyError: - if x < 2: - primes[x] = False - return False - i = 2 - while i * i <= x: - if x % i == 0: - primes[x] = False - return False - i += 1 - primes[x] = True - return True +# these are the tree and utility libraries I created +from tree import Tree +import util # TODO: tweak this to do whatever you want as I described in the Overleaf def next(x): @@ -72,7 +15,7 @@ def main(): tree = Tree(0, []) for i in range(20): print(tree.longest_path()) - tree.step() + tree.step(next) print(tree.longest_path()) diff --git a/python/tree.py b/python/tree.py new file mode 100644 index 0000000..133c23b --- /dev/null +++ b/python/tree.py @@ -0,0 +1,40 @@ +import math + +# tree stuff, I wouldn't recommend tweaking unless you have serious ideas for +# optimizations +class Tree: + def __init__(self, value, children): + self.value = value + self.children = [] + for child in children: + self.children.append(Tree(child, [])) + + def step(self, next): + if len(self.children) == 0: + for val in next(self.value): + self.children.append(Tree(val, [])) + return + for child in self.children: + child.step(next) + + def __str__(self): + retval = '[' + str(self.value) + ': ' + for child in self.children: + retval += str(child) + retval += ']' + return retval + + def __repr__(self): + return str(self) + + def longest_path(self): + if len(self.children) == 0: + return [self.value] + max_path = [] + max_length = 0 + for child in self.children: + temp = child.longest_path() + if len(temp) > max_length: + max_path = temp + max_length = len(temp) + return [self.value] + max_path diff --git a/python/util.py b/python/util.py new file mode 100644 index 0000000..e1b79eb --- /dev/null +++ b/python/util.py @@ -0,0 +1,23 @@ +import math + +# some utility functions, same as above +def is_square(x): + return int(math.sqrt(x)) == math.sqrt(x) + +primes = {} + +def is_prime(x): + try: + return primes[x] + except KeyError: + if x < 2: + primes[x] = False + return False + i = 2 + while i * i <= x: + if x % i == 0: + primes[x] = False + return False + i += 1 + primes[x] = True + return True