Compare commits

..

No commits in common. "master" and "Modification" have entirely different histories.

32 changed files with 432 additions and 3224 deletions

1
.gitignore vendored
View file

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

View file

@ -1,10 +0,0 @@
# Code from the Polymath REU Walking to Infinity on Special Sequences project
The code is organized by language so that people who knew different programming
languages would still be able to test out sequences, since the tree system I
came up with for checking every possible path of a sequence is so versatile.
Each language has an implementation of the tree system as well as a sample and a
template, so you can easily try out any sequence you want. Rust was primarily
used for the hefty calculations involving things other than the tree system,
since it is so fast and easy to use. All the tree code is well documented and
commented, and the fast code is not, but fairly straightforward.

View file

@ -1,23 +0,0 @@
# 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)

View file

@ -1,35 +0,0 @@
/* 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());
}
}

107
cpp/append_digit_primes.cpp Normal file
View file

@ -0,0 +1,107 @@
#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

@ -1,23 +0,0 @@
# 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

@ -1,28 +0,0 @@
#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());
}
}

View file

@ -1,41 +0,0 @@
#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;
}

View file

@ -1,17 +0,0 @@
#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>;
};

View file

@ -1,30 +0,0 @@
#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";
}

View file

@ -1,10 +0,0 @@
#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;

104
cpp/template.cpp Normal file
View file

@ -0,0 +1,104 @@
#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());
}
}

View file

@ -1,23 +0,0 @@
# 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)

View file

@ -1,19 +0,0 @@
/* 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());
}
}

Binary file not shown.

View file

@ -1,165 +0,0 @@
program test
implicit none
1 format(1i20)
2 format(1f23.20)
integer (kind = 8), parameter :: base = 2, chunk = 1000000
double precision :: S
integer (kind = 8) :: i, len
open(1, file = "prev.txt")
write(1,1) 1
close(1)
S = 0.5
do i = 2, 63
len = next()
call rename("array.txt", "prev.txt")
S = S + (real(len) / 2.0 ** i)
print *, "ITERATION"
write (*,1) i
print *, "NUMBER"
write (*,1) len
print *, "SUM"
write (*,2) S
print *, ""
call flush()
end do
contains
function is_fourth_free (x)
integer (kind = 8), intent (in) :: x
integer (kind = 8) :: i
logical :: is_fourth_free
i = 2
do while (i * i * i * i <= x)
if (mod(x, i * i * i * i) == 0) then
is_fourth_free = .false.
return
end if
i = i + 1
end do
is_fourth_free = .true.
end function is_fourth_free
function is_square_free (x)
integer (kind = 8), intent (in) :: x
integer (kind = 8) :: i
logical :: is_square_free
i = 2
do while (i * i <= x)
if (mod(x, i * i) == 0) then
is_square_free = .false.
return
end if
i = i + 1
end do
is_square_free = .true.
end function is_square_free
function is_prime (x)
integer (kind = 8), intent (in) :: x
integer (kind = 8) :: i
logical :: is_prime
if (x < 2 .or. mod(x, 2) == 0) then
is_prime = .false.
return
end if
i = 3
do while (i * i <= x)
if (mod(x, i) == 0) then
is_prime = .false.
return
end if
i = i + 2
end do
is_prime = .true.
end function is_prime
function step (x)
implicit none
integer (kind = 8), intent (in) :: x
integer (kind = 8) :: i, t, count
integer (kind = 8), dimension (:), allocatable :: step
integer (kind = 8), dimension (base) :: temp
count = 0
do i = 0, base - 1
t = x * base + i
if (is_fourth_free(t)) then
count = count + 1
temp(count) = t
end if
end do
allocate(step(count))
do i = 1, count
step(i) = temp(i)
end do
end function step
function next()
implicit none
1 format(1i10)
integer (kind = 8), dimension (:), allocatable :: temp, temp2
integer (kind = 8) :: current, next, i, j, templen, ios
logical :: done
templen = 0
next = 0
done = .false.
allocate(temp(chunk))
open(1, file = "array.txt")
open(2, file = "prev.txt")
do while (.not. done)
read(2, 1, iostat = ios) current
if (ios .ne. 0) then
done = .true.
exit
end if
temp2 = step(current)
do i = 1, size(temp2)
templen = templen + 1
temp(templen) = temp2(i)
next = next + 1
if (templen >= chunk) then
write(1,1) temp
call flush(1)
deallocate(temp)
allocate(temp(chunk))
templen = 0
end if
end do
deallocate(temp2)
end do
write(1,1) temp(:templen)
deallocate(temp)
close(1)
close(2)
end function next
end program test

View file

@ -1,21 +1,76 @@
import math
from tree import Tree
import util
class Tree:
def __init__(self, value, children):
self.value = value
self.children = []
for child in children:
self.children.append(Tree(child, []))
def next(x):
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):
new_xs = []
for i in range(10):
if util.is_prime(10 * x + i):
if is_prime(10 * x + i):
new_xs.append(10 * x + i)
return new_xs
def main():
tree = Tree(0, [])
for i in range(20):
for i in range(9):
print(tree.longest_path())
tree.step(next)
tree.step()
print(tree.longest_path())

View file

@ -1,16 +1,68 @@
import math
from tree import Tree
import util
class Tree:
def __init__(self, value, children):
self.value = value
self.children = []
for child in children:
self.children.append(Tree(child, []))
def next(x):
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):
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 util.is_prime(int(temp)):
if is_prime(int(temp)):
new_xs.append(int(temp))
return new_xs
@ -19,7 +71,7 @@ def main():
tree = Tree(0, [2, 3, 5, 7])
for i in range(5):
print(tree.longest_path())
tree.step(next)
tree.step()
print(tree.longest_path())
print(tree)

View file

@ -1,16 +1,53 @@
import math
from tree import Tree
import util
class Tree:
def __init__(self, value, children):
self.value = value
self.children = []
for child in children:
self.children.append(Tree(child, []))
def next(x):
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):
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 util.is_square(int(temp)):
if is_square(int(temp)):
new_xs.append(int(temp))
return new_xs
@ -18,7 +55,7 @@ def next(x):
def main():
tree = Tree(0, [1,4,9])
for i in range(20):
tree.step(next)
tree.step()
print(tree)
print(tree.longest_path())

View file

@ -1,8 +1,65 @@
import math
# these are the tree and utility libraries I created
from tree import Tree
import util
# 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
# TODO: tweak this to do whatever you want as I described in the Overleaf
def next(x):
@ -15,7 +72,7 @@ def main():
tree = Tree(0, [])
for i in range(20):
print(tree.longest_path())
tree.step(next)
tree.step()
print(tree.longest_path())

View file

@ -1,40 +0,0 @@
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

View file

@ -1,23 +0,0 @@
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

View file

@ -1,10 +0,0 @@
[package]
name = "fourthfree"
version = "0.1.0"
authors = ["William Ball <wball1@swarthmore.edu>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rayon = "1.1"

View file

@ -1,22 +0,0 @@
1 1 0.5
2 2 1
3 4 1.5
4 8 2
5 15 2.46875
6 29 2.921875
7 55 3.3515625
8 105 3.76171875
9 201 4.154296875
10 383 4.5283203125
11 733 4.88623046875
12 1401 5.228271484375
13 2678 5.55517578125
14 5119 5.86761474609375
15 9784 6.16619873046875
16 18701 6.4515533447265625
17 35738 6.724212646484375
18 68307 6.984783172607422
19 130554 7.233795166015625
20 249518 7.47175407409668
21 476910 7.699162483215332
22 911526 7.916487216949463

View file

@ -1,70 +0,0 @@
use rayon::prelude::*;
// use std::thread;
// const NUM_THREADS: usize = 12;
const BASE: u64 = 2;
fn is_fourth_free(x: &u64) -> bool {
let mut i = 2;
let mut fourth = i * i * i * i;
while fourth <= *x {
if x % fourth == 0 {
return false;
}
i += 1;
fourth = i * i * i * i;
}
true
}
fn step(x: &u64) -> Vec<u64> {
(0..BASE)
.into_iter()
.map(|d| x * BASE + d)
.filter(|d| is_fourth_free(d))
.collect()
}
fn next(ls: Vec<u64>) -> Vec<u64> {
ls.par_iter().map(step).flatten().collect()
}
// fn next(ls: Vec<u64>) -> Vec<u64> {
// let mut new: Vec<u64> = Vec::new();
// let mut children = Vec::with_capacity(NUM_THREADS);
// let size = ls.len() / NUM_THREADS;
// (0..(NUM_THREADS - 1))
// .map(|i| -> Vec<u64> { (&ls[size * i..size * (i + 1)]).iter().copied().collect() })
// .for_each(|slice| {
// children.push(thread::spawn(move || {
// slice.iter().map(|oldval| step(oldval)).flatten().collect()
// }))
// });
// children.push(thread::spawn(move || -> Vec<u64> {
// let mut new = Vec::new();
// for oldval in &ls[size * (NUM_THREADS - 1)..] {
// new.append(&mut step(oldval));
// }
// new
// }));
// for child in children {
// new.append(&mut child.join().unwrap());
// }
// new
// }
fn main() {
let mut i = 0;
let mut val: f64 = 0.0;
let mut ls: Vec<u64> = (1..BASE)
.into_iter()
.filter(|x| is_fourth_free(x))
.collect();
loop {
i += 1;
val += ls.len() as f64 / 2u64.pow(i) as f64;
println!("{}\t{}\t{}", i, ls.len(), val);
ls = next(ls);
}
}

View file

@ -1,10 +0,0 @@
[package]
name = "insert_digit_anywhere_primes_constant"
version = "0.1.0"
authors = ["William Ball <wball1@swarthmore.edu>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
primal = "0.3.0"

View file

@ -1,9 +0,0 @@
6 1.3815510557964277
7.675 1.7672340588729303
8.657602339181286 1.9934866087269214
9.287908478277238 2.1386199607174183
9.706992706401598 2.235117670356225
10.021722699613848 2.3075869294250895
10.26388157204211 2.3633460704040457
10.456005647430802 2.407584273603572
10.613201775385221 2.4437800196939956

View file

@ -1,119 +0,0 @@
use std::thread;
use std::collections::HashSet;
const NUM_THREADS: usize = 6;
// const THREAD_SKIP: u64 = 1_000;
const BASE: u64 = 10;
fn remove_duplicates(ls: Vec<u64>) -> Vec<u64> {
let mut hs: HashSet<u64> = HashSet::new();
for val in &ls {
hs.insert(*val);
}
hs.iter().map(|x| *x).collect()
}
fn u64_to_v(x: u64) -> Vec<u64> {
let mut ll = Vec::new();
let mut x = x;
while x > 0 {
ll.push(x % BASE);
x /= BASE;
}
ll
}
fn v_to_u64(ll: Vec<u64>) -> u64 {
let mut val = 0;
let mut ll = ll;
loop {
match ll.pop() {
Some(v) => {
val += v;
val *= BASE;
}
None => break,
};
}
val / BASE
}
fn step(x: u64) -> Vec<u64> {
let mut new_xs = Vec::new();
let v_x = u64_to_v(x);
for i in 0..v_x.len() + 1 {
for d in 0..BASE {
let mut v_x = u64_to_v(x);
v_x.insert(i, d);
let temp = v_to_u64(v_x);
if !(i == 0 && d == 0) {
if primal::is_prime(temp) {
new_xs.push(temp);
}
}
}
}
new_xs
}
fn next(ls: Vec<u64>) -> (f64, Vec<u64>) {
let old = ls.len();
let mut count = 0;
let mut new: Vec<u64> = Vec::new();
let mut slices: Vec<Vec<u64>> = Vec::new();
let mut children = Vec::with_capacity(NUM_THREADS);
let size = ls.len() / NUM_THREADS;
for i in 0..(NUM_THREADS - 1) {
let mut new = Vec::with_capacity(size);
for val in ls.iter().skip(size * i).take(size) {
new.push(*val);
}
slices.push(new);
}
{
let mut new = Vec::new();
for val in ls.iter().skip(size * (NUM_THREADS - 1)) {
new.push(*val);
}
slices.push(new);
}
for slice in slices {
children.push(thread::spawn(move || -> (u64, Vec<u64>) {
let mut new = Vec::new();
let mut count = 0;
for oldval in slice {
let mut temp = step(oldval);
// let temp = step(oldval);
count += temp.len() as u64;
// new.push(oldval + THREAD_SKIP);
new.append(&mut temp);
}
(count, new)
}));
}
for child in children {
let (temp1, mut temp2) = child.join().unwrap();
new.append(&mut temp2);
count += temp1;
}
(count as f64 / old as f64, remove_duplicates(new))
}
fn main() {
// let mut ls: Vec<u64> = (1..(NUM_THREADS as u64) * THREAD_SKIP)
// .into_iter()
// .collect();
let mut count: f64;
let mut i = 0;
let mut total: f64 = 0.0;
let mut ls = primal::Primes::all().take_while(|x| *x < BASE as usize).map(|x| x as u64).collect();
loop {
let res = next(ls);
count = res.0;
ls = res.1;
i += 1;
total += count;
let res = total as f64 / i as f64;
println!("{}\t{}", res, f64::abs(res / (BASE as f64 / f64::ln(BASE as f64))));
}
}

View file

@ -1,11 +0,0 @@
[package]
name = "insert_digit_anywhere_primes_constant"
version = "0.1.0"
authors = ["William Ball <wball1@swarthmore.edu>"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
is_prime = "2.0.7"
rand = "*"

File diff suppressed because it is too large Load diff

View file

@ -1,71 +0,0 @@
use is_prime::is_prime;
use rand::prelude::*;
use std::io::prelude::*;
fn step(x: String, rng: &mut ThreadRng) -> Option<String> {
let mut count = 0;
loop {
count += 1;
if count > 10 * x.len() {
return None;
}
let i = rng.gen_range(0, x.len() + 1);
let d = rng.gen_range(0, 10) as u8;
let mut copy = x.clone();
copy.insert(i, (d + '0' as u8) as char);
if copy.as_bytes()[0] != '0' as u8 {
if is_prime(&copy) {
return Some(copy);
}
}
}
}
fn main() {
let mut rng = thread_rng();
let mut x;
match rng.gen_range(0, 4) {
0 => x = String::from("2"),
1 => x = String::from("3"),
2 => x = String::from("5"),
3 => x = String::from("7"),
_ => x = String::from("0"),
}
let mut length = 1;
println!("{}", x);
loop {
match step(x, &mut rng) {
Some(val) => {
length += 1;
println!("{}", val);
std::io::stdout()
.flush()
.ok()
.expect("Could not flush stdout");
x = val;
if length > 100 {
length = 1;
match rng.gen_range(0, 4) {
0 => x = String::from("2"),
1 => x = String::from("3"),
2 => x = String::from("5"),
3 => x = String::from("7"),
_ => x = String::from("0"),
}
println!("{}", x);
}
}
None => {
println!("\n\nUnsucessful! Only length: {}\n\n\n", length);
length = 1;
match rng.gen_range(0, 4) {
0 => x = String::from("2"),
1 => x = String::from("3"),
2 => x = String::from("5"),
3 => x = String::from("7"),
_ => x = String::from("0"),
}
}
}
}
}