minor tweaks

This commit is contained in:
William Ball 2020-07-17 22:03:58 -07:00
parent e27728e730
commit b9ae16be7a
5 changed files with 129 additions and 30 deletions

View file

@ -26,10 +26,10 @@ public:
for (auto val : xs) {
children.push_back(Tree(val, {}));
}
return;
}
for (auto &child : children) {
child.step();
} else {
for (auto &child : children) {
child.step();
}
}
}
@ -53,7 +53,7 @@ public:
};
auto next(ull x) -> std::vector<ull> {
std::vector<ull> new_xs();
std::vector<ull> new_xs;
ull temp = x * 10;
for (int i = 0; i < 10; i++) {
auto temp2 = temp + i;
@ -66,6 +66,10 @@ auto next(ull x) -> std::vector<ull> {
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];
}
@ -89,14 +93,14 @@ auto print_vec(std::vector<T> vec) -> void {
}
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, primes);
for (int i = 0; i < 200; i++) {
/* 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,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()

View file

@ -71,15 +71,15 @@ fn step(x: u64) -> Vec<u64> {
fn main() {
/* find all non-truncatable primes in (100,000,000; 1,000,000,000) */
let primes: Vec<u64> = primal::Primes::all()
.skip_while(|x| *x < 100_000_000)
.take_while(|x| *x < 1_000_000_000)
.map(|x| x as u64)
.filter(|x| !(primal::is_prime(*x / 10)))
.collect();
// let primes: Vec<u64> = primal::Primes::all()
// .skip_while(|x| *x < 100_000_000)
// .take_while(|x| *x < 1_000_000_000)
// .map(|x| x as u64)
// .filter(|x| !(primal::is_prime(*x / 10)))
// .collect();
let mut tree = Tree::new(0, primes);
for _ in 0..20 {
let mut tree = Tree::new(0, Vec::new());
for _ in 0..9 {
tree.step();
println!("{:?}", tree.longest_path());
}

View file

@ -1,13 +1,5 @@
(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 (children tree) (cdr tree))
(define (new-tree val children)
@ -31,7 +23,9 @@
'()
(map longest-path (children tree))))))))
; --- change this function to control behavior of tree ---
(define (next val)
(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)))))

View file

@ -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)
(if (null? seq)
init
@ -18,3 +38,6 @@
(if (= i n)
func
(loop (+ i 1) (compose func f)))))
(define (digits n)
(length (string->list (number->string n))))