From c343e9938712fc7d6a71011d3bcc8166d655e90a Mon Sep 17 00:00:00 2001 From: William Ball Date: Wed, 8 Jul 2020 22:24:26 -0700 Subject: [PATCH] non_squarefree & cleaning up --- .gitignore | 3 + .../insert_digit_anywhere.cpython-38.pyc | Bin 2054 -> 0 bytes non_squarefree/Cargo.toml | 10 ++ non_squarefree/src/main.rs | 113 ++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 .gitignore delete mode 100644 __pycache__/insert_digit_anywhere.cpython-38.pyc create mode 100644 non_squarefree/Cargo.toml create mode 100644 non_squarefree/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aee3c82 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +*/Cargo.lock +*/target/* diff --git a/__pycache__/insert_digit_anywhere.cpython-38.pyc b/__pycache__/insert_digit_anywhere.cpython-38.pyc deleted file mode 100644 index e9138944403dad252c3709189ed1e36da66ea931..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 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 diff --git a/non_squarefree/Cargo.toml b/non_squarefree/Cargo.toml new file mode 100644 index 0000000..3482c62 --- /dev/null +++ b/non_squarefree/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "non_squarefree" +version = "0.1.0" +authors = ["William Ball "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +primal = "0.3.0" diff --git a/non_squarefree/src/main.rs b/non_squarefree/src/main.rs new file mode 100644 index 0000000..04e6810 --- /dev/null +++ b/non_squarefree/src/main.rs @@ -0,0 +1,113 @@ +/* An attempt to see if you can walk to infinity on non-squarefree numbers + * The answer is a very obvious "yes" I should have seen coming. + * 4 -> 44 -> 444 -> 4444 -> ... + * will always be divisible by 4 and thus will always be non-squarefree + * Similarly + * 9 -> 99 -> 999 -> 9999 -> ... + * will always be divisible by 9. + */ + +use std::collections::HashMap; + +#[derive(Debug)] +struct Tree { + value: u64, + children: Vec, +} + +impl Tree { + fn new(value: u64, children: Vec) -> Tree { + let mut tree_children = Vec::with_capacity(children.len()); + for child in children { + tree_children.push(Tree::new(child, Vec::new())); + } + Tree { + value, + children: tree_children, + } + } + + fn step(&mut self, square_free: &mut HashMap) { + if self.children.is_empty() { + let xs = step_end(self.value, square_free); + for val in xs { + self.children.push(Tree::new(val, Vec::new())); + } + return; + } + for child in &mut self.children { + child.step(square_free); + } + } + + fn longest_path(&self) -> Vec { + if self.children.is_empty() { + return vec![self.value]; + } + let mut max_path = vec![]; + let mut max_length = 0; + for child in &self.children { + let temp = child.longest_path(); + if temp.len() > max_length { + max_length = temp.len(); + max_path = temp; + } + } + let mut retval = vec![self.value]; + retval.append(&mut max_path); + return retval; + } + + fn to_string(&self) -> String { + let mut retval = format!("[{}: ", self.value); + for child in &self.children { + retval.push_str(&child.to_string()[..]); + } + retval.push(']'); + retval + } + + fn count(&self) -> usize { + if self.children.is_empty() { + return 1; + } + return self.children.iter().fold(0, |mut sum, x| { sum += x.count(); sum }); + } +} + +fn step_end(x: u64, square_free: &mut HashMap) -> Vec { + let mut new_xs = Vec::new(); + for d in 0..10 { + let temp = x * 10 + d; + if !is_squarefree(temp, square_free) { + new_xs.push(temp); + } + } + return new_xs; +} + +fn is_squarefree(n: u64, square_free: &mut HashMap) -> bool { + match square_free.get(&n) { + Some(val) => *val, + None => { + let n = n as usize; + for prime in primal::Primes::all().take(n) { + if n % (prime * prime) == 0 { + return false; + } + } + true + } + } +} + +fn main() { + let mut tree = Tree::new(0, vec![4, 9]); + let mut square_free: HashMap = HashMap::new(); + for _ in 0..10 { + tree.step(&mut square_free); + println!("{:?}", tree.longest_path()); + println!("{}", tree.count()); + } + println!("{:?}", tree.to_string()); +}