added add_digit_anywhere_primes to c++
This commit is contained in:
parent
6bf8205823
commit
1eba11c04b
3 changed files with 58 additions and 0 deletions
23
cpp/add_digit_anywhere_primes/Makefile
Normal file
23
cpp/add_digit_anywhere_primes/Makefile
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# rename `template` to name of project
|
||||
TARGET=add_digit_anywhere_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)
|
||||
35
cpp/add_digit_anywhere_primes/add_digit_anywhere_primes.cpp
Normal file
35
cpp/add_digit_anywhere_primes/add_digit_anywhere_primes.cpp
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/* include little library I made */
|
||||
#include "../common/util.h"
|
||||
#include "../common/tree.h"
|
||||
#include <string>
|
||||
|
||||
auto next(ull x) -> std::vector<ull> {
|
||||
std::vector<ull> new_xs;
|
||||
ull temp;
|
||||
std::string str_x = std::to_string(x);
|
||||
for (int i = 0; i < str_x.length(); i++) {
|
||||
for (int d = 0; d < 10; d++) {
|
||||
if (!(i == 0 && d == 0)) {
|
||||
str_x = std::to_string(x);
|
||||
str_x.insert(i, std::to_string(d));
|
||||
/* std::cout << x << ' ' << str_x << ' ' << i << ' ' << d << '\n'; */
|
||||
temp = std::stoull(str_x);
|
||||
if (is_prime(temp)) {
|
||||
new_xs.push_back(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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, {2, 3, 5, 7});
|
||||
for (int i = 0; i < 20; i++) {
|
||||
tree.step(next);
|
||||
print_vec(tree.longest_path());
|
||||
}
|
||||
}
|
||||
BIN
cpp/append_digit_primes/append_digit_primes
Executable file
BIN
cpp/append_digit_primes/append_digit_primes
Executable file
Binary file not shown.
Loading…
Reference in a new issue