primes
This commit is contained in:
parent
9199088189
commit
a46cc293fc
3 changed files with 80 additions and 2 deletions
BIN
__pycache__/insert_digit_anywhere.cpython-38.pyc
Normal file
BIN
__pycache__/insert_digit_anywhere.cpython-38.pyc
Normal file
Binary file not shown.
79
insert_digit_anywhere_primes.py
Normal file
79
insert_digit_anywhere_primes.py
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
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:
|
||||||
|
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 is_prime(int(temp)):
|
||||||
|
new_xs.append(int(temp))
|
||||||
|
return new_xs
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
tree = Tree(0, [2, 3, 5, 7])
|
||||||
|
for i in range(20):
|
||||||
|
print(tree.longest_path())
|
||||||
|
tree.step()
|
||||||
|
print(tree.longest_path())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
@ -21,7 +21,6 @@ class Tree:
|
||||||
retval += str(child)
|
retval += str(child)
|
||||||
retval += ']'
|
retval += ']'
|
||||||
return retval
|
return retval
|
||||||
#return str(self.value) + ': ' + str([str(child) for child in self.children])
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return str(self)
|
return str(self)
|
||||||
|
|
@ -55,7 +54,7 @@ def step(x):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
tree = Tree(0, [1,4,9])
|
tree = Tree(0, [1,4,9])
|
||||||
for i in range(200):
|
for i in range(20):
|
||||||
tree.step()
|
tree.step()
|
||||||
print(tree)
|
print(tree)
|
||||||
print(tree.longest_path())
|
print(tree.longest_path())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue