minor tweaks
This commit is contained in:
parent
e27728e730
commit
b9ae16be7a
5 changed files with 129 additions and 30 deletions
|
|
@ -26,12 +26,12 @@ public:
|
||||||
for (auto val : xs) {
|
for (auto val : xs) {
|
||||||
children.push_back(Tree(val, {}));
|
children.push_back(Tree(val, {}));
|
||||||
}
|
}
|
||||||
return;
|
} else {
|
||||||
}
|
|
||||||
for (auto &child : children) {
|
for (auto &child : children) {
|
||||||
child.step();
|
child.step();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto longest_path() -> std::vector<ull> {
|
auto longest_path() -> std::vector<ull> {
|
||||||
if (children.size() == 0) {
|
if (children.size() == 0) {
|
||||||
|
|
@ -53,7 +53,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
auto next(ull x) -> std::vector<ull> {
|
auto next(ull x) -> std::vector<ull> {
|
||||||
std::vector<ull> new_xs();
|
std::vector<ull> new_xs;
|
||||||
ull temp = x * 10;
|
ull temp = x * 10;
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
auto temp2 = temp + i;
|
auto temp2 = temp + i;
|
||||||
|
|
@ -66,6 +66,10 @@ auto next(ull x) -> std::vector<ull> {
|
||||||
|
|
||||||
auto is_prime(ull n) -> bool {
|
auto is_prime(ull n) -> bool {
|
||||||
static std::unordered_map<ull, bool> primes;
|
static std::unordered_map<ull, bool> primes;
|
||||||
|
if (n < 2) {
|
||||||
|
primes[n] = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (primes.count(n) > 0) {
|
if (primes.count(n) > 0) {
|
||||||
return primes[n];
|
return primes[n];
|
||||||
}
|
}
|
||||||
|
|
@ -89,14 +93,14 @@ auto print_vec(std::vector<T> vec) -> void {
|
||||||
}
|
}
|
||||||
|
|
||||||
auto main() -> int {
|
auto main() -> int {
|
||||||
std::vector<ull> primes;
|
/* std::vector<ull> primes; */
|
||||||
for (int i = 2; i < 10000000; i++) {
|
/* for (int i = 2; i < 10000000; i++) { */
|
||||||
if (is_prime(i)) {
|
/* if (is_prime(i)) { */
|
||||||
primes.push_back(i);
|
/* primes.push_back(i); */
|
||||||
}
|
/* } */
|
||||||
}
|
/* } */
|
||||||
Tree tree(0, primes);
|
Tree tree(0, {});
|
||||||
for (int i = 0; i < 200; i++) {
|
for (int i = 0; i < 9; i++) {
|
||||||
tree.step();
|
tree.step();
|
||||||
print_vec(tree.longest_path());
|
print_vec(tree.longest_path());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
78
python/append_digit_primes.py
Normal file
78
python/append_digit_primes.py
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
import math
|
||||||
|
|
||||||
|
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 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 is_prime(10 * x + i):
|
||||||
|
new_xs.append(10 * x + i)
|
||||||
|
return new_xs
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
tree = Tree(0, [])
|
||||||
|
for i in range(9):
|
||||||
|
print(tree.longest_path())
|
||||||
|
tree.step()
|
||||||
|
print(tree.longest_path())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
@ -71,15 +71,15 @@ fn step(x: u64) -> Vec<u64> {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
/* find all non-truncatable primes in (100,000,000; 1,000,000,000) */
|
/* find all non-truncatable primes in (100,000,000; 1,000,000,000) */
|
||||||
let primes: Vec<u64> = primal::Primes::all()
|
// let primes: Vec<u64> = primal::Primes::all()
|
||||||
.skip_while(|x| *x < 100_000_000)
|
// .skip_while(|x| *x < 100_000_000)
|
||||||
.take_while(|x| *x < 1_000_000_000)
|
// .take_while(|x| *x < 1_000_000_000)
|
||||||
.map(|x| x as u64)
|
// .map(|x| x as u64)
|
||||||
.filter(|x| !(primal::is_prime(*x / 10)))
|
// .filter(|x| !(primal::is_prime(*x / 10)))
|
||||||
.collect();
|
// .collect();
|
||||||
|
|
||||||
let mut tree = Tree::new(0, primes);
|
let mut tree = Tree::new(0, Vec::new());
|
||||||
for _ in 0..20 {
|
for _ in 0..9 {
|
||||||
tree.step();
|
tree.step();
|
||||||
println!("{:?}", tree.longest_path());
|
println!("{:?}", tree.longest_path());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,5 @@
|
||||||
(load "util.scm")
|
(load "util.scm")
|
||||||
|
|
||||||
(define (prime? n)
|
|
||||||
(if (< n 2) #f
|
|
||||||
(let loop ((i 2))
|
|
||||||
(cond
|
|
||||||
((> (* i i) n) #t)
|
|
||||||
((zero? (remainder n i)) #f)
|
|
||||||
(else (loop (+ i 1)))))))
|
|
||||||
|
|
||||||
(define (val tree) (car tree))
|
(define (val tree) (car tree))
|
||||||
(define (children tree) (cdr tree))
|
(define (children tree) (cdr tree))
|
||||||
(define (new-tree val children)
|
(define (new-tree val children)
|
||||||
|
|
@ -31,7 +23,9 @@
|
||||||
'()
|
'()
|
||||||
(map longest-path (children tree))))))))
|
(map longest-path (children tree))))))))
|
||||||
|
|
||||||
|
; --- change this function to control behavior of tree ---
|
||||||
(define (next val)
|
(define (next val)
|
||||||
(filter prime? (map (lambda (d) (+ (* val 10) d)) (range 10))))
|
(filter prime? (map (lambda (d) (+ (* val 10) d)) (range 10))))
|
||||||
|
|
||||||
; (define big-tree (tree 0 (filter prime? (range 1000000))))
|
; (define (next val)
|
||||||
|
; (filter prime? (map (lambda (d) (+ (* d (expt 10 (digits val))) val)) (cdr (range 10)))))
|
||||||
|
|
@ -1,3 +1,23 @@
|
||||||
|
(define (square? n)
|
||||||
|
(let ((s (sqrt n)))
|
||||||
|
(= s (floor s))))
|
||||||
|
|
||||||
|
(define (prime? n)
|
||||||
|
(if (< n 2) #f
|
||||||
|
(let loop ((i 2))
|
||||||
|
(cond
|
||||||
|
((> (* i i) n) #t)
|
||||||
|
((zero? (remainder n i)) #f)
|
||||||
|
(else (loop (+ i 1)))))))
|
||||||
|
|
||||||
|
(define (square-free? n)
|
||||||
|
(if (< n 1) #f
|
||||||
|
(let loop ((i 2))
|
||||||
|
(cond
|
||||||
|
((> (* i i) n) #t)
|
||||||
|
((zero? (remainder n (* i i))) #f)
|
||||||
|
(else (loop (+ i 1)))))))
|
||||||
|
|
||||||
(define (fold f init seq)
|
(define (fold f init seq)
|
||||||
(if (null? seq)
|
(if (null? seq)
|
||||||
init
|
init
|
||||||
|
|
@ -18,3 +38,6 @@
|
||||||
(if (= i n)
|
(if (= i n)
|
||||||
func
|
func
|
||||||
(loop (+ i 1) (compose func f)))))
|
(loop (+ i 1) (compose func f)))))
|
||||||
|
|
||||||
|
(define (digits n)
|
||||||
|
(length (string->list (number->string n))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue