a little more cleaning before everyone else joins

This commit is contained in:
William Ball 2020-07-19 20:49:06 -07:00
parent e3b6d583b2
commit 32b49348e9
17 changed files with 275 additions and 432 deletions

1
.gitignore vendored
View file

@ -2,5 +2,6 @@ python/__pycache__
rust/*/Cargo.lock
rust/*/target/*
cpp/a.out
cpp/*/*.o
java/*/*.class
scheme/*.so

View file

@ -1,107 +0,0 @@
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using ull = unsigned long long;
auto next(ull x) -> std::vector<ull>;
auto is_prime(ull n) -> bool;
class Tree
{
public:
ull value;
std::vector<Tree> children;
Tree(ull value, std::vector<ull> 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<ull> {
if (children.size() == 0) {
return { value };
}
std::vector<ull> 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<ull> retval = { value };
retval.insert(retval.end(), max_path.begin(), max_path.end());
return retval;
}
};
auto next(ull x) -> std::vector<ull> {
std::vector<ull> 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<ull, bool> 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 <typename T>
auto print_vec(std::vector<T> vec) -> void {
std::cout << '[';
for (T &val : vec) {
std::cout << val << ", ";
}
std::cout << "]\n";
}
auto main() -> int {
/* std::vector<ull> 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());
}
}

View file

@ -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)

View file

@ -0,0 +1,28 @@
#include "../common/util.h"
#include "../common/tree.h"
#include <cstdlib>
auto next(ull x) -> std::vector<ull> {
std::vector<ull> 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 <search_depth>\n";
return -1;
}
Tree tree(0, {});
for (int i = 0; i < std::atoi(argv[1]); i++) {
tree.step(next);
print_vec(tree.longest_path());
}
}

41
cpp/common/tree.cpp Normal file
View file

@ -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<ull> tree_children) : value(value) {
for (ull child : tree_children) {
children.push_back(Tree(child, {}));
}
}
void Tree::step(std::vector<ull> (*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<ull> {
if (children.size() == 0) {
return { value };
}
std::vector<ull> 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<ull> retval = { value };
retval.insert(retval.end(), max_path.begin(), max_path.end());
return retval;
}

17
cpp/common/tree.h Normal file
View file

@ -0,0 +1,17 @@
#include <vector>
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<Tree> children;
Tree(ull value, std::vector<ull> tree_children);
void step(std::vector<ull> (*next)(ull));
auto longest_path() -> std::vector<ull>;
};

30
cpp/common/util.cpp Normal file
View file

@ -0,0 +1,30 @@
#include "util.h"
/* some helper functions */
auto is_prime(ull n) -> bool {
static std::unordered_map<ull, bool> 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<ull> vec) -> void {
std::cout << '[';
for (ull val : vec) {
std::cout << val << ", ";
}
std::cout << "]\n";
}

10
cpp/common/util.h Normal file
View file

@ -0,0 +1,10 @@
#include <vector>
#include <unordered_map>
#include <iostream>
using ull = unsigned long long;
/* some helper functions */
auto is_prime(ull n) -> bool;
auto print_vec(std::vector<ull> vec) -> void;

View file

@ -1,104 +0,0 @@
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using ull = unsigned long long;
auto next(ull x) -> std::vector<ull>;
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<Tree> children;
Tree(ull value, std::vector<ull> 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<ull> {
if (children.size() == 0) {
return { value };
}
std::vector<ull> 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<ull> 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<ull> {
std::vector<ull> new_xs;
return new_xs;
}
/* some helper functions */
auto is_prime(ull n) -> bool {
static std::unordered_map<ull, bool> 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 <typename T>
auto print_vec(std::vector<T> 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());
}
}

23
cpp/template/Makefile Normal file
View file

@ -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)

19
cpp/template/template.cpp Normal file
View file

@ -0,0 +1,19 @@
/* include little library I made */
#include "../common/util.h"
#include "../common/tree.h"
auto next(ull x) -> std::vector<ull> {
std::vector<ull> 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());
}
}

View file

@ -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())

View file

@ -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)

View file

@ -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())

View file

@ -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())

40
python/tree.py Normal file
View file

@ -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

23
python/util.py Normal file
View file

@ -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