From a46cc293fc83db55f1cc8c68cdda246e1e87b0b2 Mon Sep 17 00:00:00 2001 From: William Ball Date: Sat, 4 Jul 2020 13:37:07 -0700 Subject: [PATCH] primes --- .../insert_digit_anywhere.cpython-38.pyc | Bin 0 -> 2054 bytes insert_digit_anywhere_primes.py | 79 ++++++++++++++++++ insert_digit_anywhere_squares.py | 3 +- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 __pycache__/insert_digit_anywhere.cpython-38.pyc create mode 100644 insert_digit_anywhere_primes.py diff --git a/__pycache__/insert_digit_anywhere.cpython-38.pyc b/__pycache__/insert_digit_anywhere.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e9138944403dad252c3709189ed1e36da66ea931 GIT binary patch literal 2054 zcmZ`)U2hvj6rGu!U9VFo3ZWz^A5uX}suD=NAiorNFtj5#GdON+$DZAg zG+IkYF8?EW>_6cT@UpKw`7h*&b7tdw6fxG`xwA8S=iYPAz5Z!=c}ZaSzlHfa5#k>% zu8#lW|H$c?kofYc z$ZpMHMYMM*uMdUA6Fm8S2q$2Z_M}z8USu`x+1MtSW81JP=EOGb66S`z1B5%c02h{!jfR}s+ibUlP$3)0K4Kv2nB5JIltcUx+?Bu|t`qnpaKQf2~!U@_W+5N0IN>+7MLHM zMrUHGabC|*5Bu70m3m4rv7V~QR{d=$H$W24)MeT)V+u?NI99E}!Kml*!i>T0EDjNM z(qZ>Fyyz@Z19y7GULZ!L3pX6U6|#R1Hb2CZ--aMPQjml6NM)<0$vXk+>vy%Ylu(Vj zLuX92WX$lu9uBCtjF}vE2lbA>1&e`y{#|OkA>4P(2iB?&JSE!1GGscHX_BUCBaH&d zZ-CAOU-EMki$WA=Bc(V)?Jcx@wMaOa*B=#?W((-(eBz5tR`U3o54^r=_H&cNcke!< z6Ls$WN6>FvPS#u&{U2B`w>872sz4?!G8T&d#jk1_A;XIOdd6~TPh;$hGWt{9AyeWR z&X%f-tl%3IRsvn2c|0#;&iQr`6}n7j2+VJn@-(hjp;KP`M z8yyav?-s6#eHT0lvH16K#4~`b;hFYMGIB@eIPb))q~l8V(d5}6D{1{Y4~Gn0rHSq2 zb38fANPrK<^JB6$`_geHi_a?u1g z@vi_JrQm$3&w|dzm?d>%oRbD~@?cKq=6^w;26&tQn1YU0@z^8zfpnSb=mO2$f5P3H zQIQ?}(F1H;^ce=j67U(Vfa}~h2H<|cufr|mM$K*>@*5OaW`h=(l|HJ=<1sxBT{u-M LpQcgT0>kCMHvN7W literal 0 HcmV?d00001 diff --git a/insert_digit_anywhere_primes.py b/insert_digit_anywhere_primes.py new file mode 100644 index 0000000..1a4cba3 --- /dev/null +++ b/insert_digit_anywhere_primes.py @@ -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() diff --git a/insert_digit_anywhere_squares.py b/insert_digit_anywhere_squares.py index cbca191..9b181cf 100644 --- a/insert_digit_anywhere_squares.py +++ b/insert_digit_anywhere_squares.py @@ -21,7 +21,6 @@ class Tree: retval += str(child) retval += ']' return retval - #return str(self.value) + ': ' + str([str(child) for child in self.children]) def __repr__(self): return str(self) @@ -55,7 +54,7 @@ def step(x): def main(): tree = Tree(0, [1,4,9]) - for i in range(200): + for i in range(20): tree.step() print(tree) print(tree.longest_path())