diff --git a/README.md b/README.md deleted file mode 100644 index 250e59d..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# code_games - -its muh code games \ No newline at end of file diff --git a/fibonacci/Cargo.toml b/fibonacci/Cargo.toml new file mode 100755 index 0000000..9cd048e --- /dev/null +++ b/fibonacci/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "fibonacci" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/fibonacci/src/main.rs b/fibonacci/src/main.rs new file mode 100755 index 0000000..c587145 --- /dev/null +++ b/fibonacci/src/main.rs @@ -0,0 +1,37 @@ +fn main() { + let fib_instance = FibItr::default(); + println!("{}", + fib_instance + .filter(is_even) + .take_while(|x| *x < 4000000) + .sum::() + ); +} + +fn is_even(x: &usize) -> bool { + x % 2 == 0 +} + +struct FibItr { + index: usize, + prev: usize +} + +impl Iterator for FibItr { + type Item = usize; + fn next ( &mut self ) -> Option { + let result = self.index + self.prev; + self.prev = self.index; + self.index = result; + return Some(result); + } +} + +impl Default for FibItr { + fn default() -> Self { + FibItr { + index: 1, + prev: 1 + } + } +} diff --git a/guess/Cargo.toml b/guess/Cargo.toml new file mode 100755 index 0000000..fb1ec2c --- /dev/null +++ b/guess/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/guess/src/main.rs b/guess/src/main.rs new file mode 100755 index 0000000..48c93b1 --- /dev/null +++ b/guess/src/main.rs @@ -0,0 +1,19 @@ +use std::io; +use std::io::Write; + +fn main() { + let mut theguess_tm = String::new(); + + println!("Hello noob"); + print!("Guess a number or something lol: "); + + io::stdout() + .flush() + .expect("couldn't flush ?"); + + io::stdin() + .read_line(&mut theguess_tm) + .expect("Stinky computer lol ur mom"); + + println!("ur guess was: {}", theguess_tm); +} diff --git a/line_racing/Cargo.toml b/line_racing/Cargo.toml new file mode 100644 index 0000000..dbe92dd --- /dev/null +++ b/line_racing/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "line_racing" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/line_racing/src/main.rs b/line_racing/src/main.rs new file mode 100644 index 0000000..3f4b8b2 --- /dev/null +++ b/line_racing/src/main.rs @@ -0,0 +1,33 @@ +use std::io; + +macro_rules! parse_input { + ($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap()) +} + +fn main() { + loop { + let mut input_line = String::new(); + + io::stdin().read_line(&mut input_line).unwrap(); + + let inputs = input_line.split(" ").collect::>(); + + let n = parse_input!(inputs[0], usize); // total number of players (2 to 4). + let p = parse_input!(inputs[1], usize); // your player number (0 to 3). + + for i in 0..n as usize { + let mut input_line = String::new(); + io::stdin().read_line(&mut input_line).unwrap(); + let inputs = input_line.split(" ").collect::>(); + let x0 = parse_input!(inputs[0], usize); // starting X coordinate of lightcycle (or -1) + let y0 = parse_input!(inputs[1], usize); // starting Y coordinate of lightcycle (or -1) + let x1 = parse_input!(inputs[2], usize); // starting X coordinate of lightcycle (can be the same as X0 if you play before this player) + let y1 = parse_input!(inputs[3], usize); // starting Y coordinate of lightcycle (can be the same as Y0 if you play before this player) + } + + // Write an action using println!("message..."); + // To debug: eprintln!("Debug message..."); + + println!("LEFT"); // A single line with UP, DOWN, LEFT or RIGHT + } +} diff --git a/multiples/Cargo.toml b/multiples/Cargo.toml new file mode 100755 index 0000000..3944cb1 --- /dev/null +++ b/multiples/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "multiples" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/multiples/src/main.rs b/multiples/src/main.rs new file mode 100755 index 0000000..020b4e1 --- /dev/null +++ b/multiples/src/main.rs @@ -0,0 +1,15 @@ +fn main() { + let mut piss_list: Vec = Vec::new(); + let numbers = 1..1000; + + for x in numbers { + if x % 3 == 0 { + piss_list.push(x); + } + else if x % 5 == 0 { + piss_list.push(x); + } + } + let total: usize = piss_list.into_iter().sum(); + println!("ur sum esssssssssssssssssssssss uhhh: {} ", total); +} diff --git a/palindrome/Cargo.toml b/palindrome/Cargo.toml new file mode 100755 index 0000000..140de6a --- /dev/null +++ b/palindrome/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "palindrome" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/palindrome/src/main.rs b/palindrome/src/main.rs new file mode 100755 index 0000000..5b61b86 --- /dev/null +++ b/palindrome/src/main.rs @@ -0,0 +1,157 @@ +use std::iter::Peekable; + +const ERR_TXT: &str = "unexpected item in bagging area"; + +fn main() { +// let mut something: Option<(usize,usize)> = None; +// for x in (100..999).rev() { +// for y in (100..999).rev() { +// if reverse( x * y ) == x * y { +// something = Some(( x, y )); +// break +// } +// else { +// something = None +// } +// } +// if !something.is_none() { +// break +// } +// } +// println!("{} * {} = {}", something.expect("").0, something.expect("").1, something.expect("").0 * something.expect("").1) + +// let palindrome = (0..999*999).rev() +// .filter( |x| *x == reverse(*x)) +// .find( |x| palin_valid(*x) ) +// .expect(ERR_TXT); +// println!("{}", palindrome); + +let palindrome = (0..999*999).rev() + .filter( |x| *x == reverse(*x)) + .filter( |x| palin_valid(*x, false)) + .take(100); +for x in palindrome { + println!("{}, {}", x, palin_valid(x, true)); +} + +// println!("{}", palin_valid(906609, true)) +} + +fn palin_valid(palin: usize, debug: bool) -> bool { + let mut x: usize = 1; + let mut y: usize = 1; + let mut x_check = true; + let factors = PrimeFactItr::factors(palin).collect::>().into_iter().rev(); + for factor in factors { + if debug { println!("x : {}, y : {}, factor : {}", x, y, factor); } + if x_check { + if x * factor <= 999 { + x *= factor; + } + else if y * factor <= 999 { + y *= factor; + } + else { + if debug { println!("x* : {}, y* : {}, factor : {}", x*factor, y*factor, factor); } + return false; + } + } + else { + if y * factor <= 999 { + y *= factor; + } + else if x * factor <= 999 { + x *= factor; + } + else { + if debug { println!("x* : {}, y* : {}, factor : {}", x * factor, y * factor, factor); } + return false; + } + } + x_check = !x_check; + } + if debug { println!("x : {}, y : {}", x, y); } + return true; +} + +fn reverse(mut n:usize) -> usize { + let radix = 10; + let mut reversed = 0; + while n != 0 { + reversed = reversed * radix + n % radix; + n /= radix; + } + reversed +} + +//prime iterator + +struct PrimeItr { + record: Vec, + index: usize +} + +impl Iterator for PrimeItr { + type Item = usize; + fn next ( &mut self ) -> Option { + let is_prime = !self.record.iter() + .take_while( |x| **x * **x < self.index ) + .any( |x| self.index % *x == 0 ); + let result = self.index; + self.index += if self.index == 2 { 1 } else { 2 }; + if is_prime { + self.record.push(result); + Some(result) + } + else { + self.next() + } + } +} + +impl Default for PrimeItr { + fn default() -> Self { + PrimeItr { + record: vec![], + index: 2 + } + } +} + +// prime factorization iterator + +struct PrimeFactItr { + record: Vec, + chuck: usize, + prime: Peekable +} + +impl Iterator for PrimeFactItr { + type Item = usize; + fn next ( &mut self ) -> Option { + let x = *self.prime.peek().expect(ERR_TXT); + let is_factor = self.chuck % x == 0; + if is_factor { + self.record.push(x); + self.chuck /= x; + Some(x) + } + else if x > self.chuck { + None + } + else { + self.prime.next(); + self.next() + } + } +} + +impl PrimeFactItr { + fn factors(x: usize) -> Self { + PrimeFactItr { + record: vec![], + chuck: x, + prime: PrimeItr::default().peekable() + } + } +} diff --git a/primes.py b/primes.py new file mode 100755 index 0000000..e59053c --- /dev/null +++ b/primes.py @@ -0,0 +1,15 @@ +primes = [2] +counter = 1 +while counter <= 2000000: + counter += 2 + for x in primes: + if x * x > counter: + # print(str(counter) + " found") + primes += [counter] + break + elif counter % x == 0: + break +counter = 0 +for x in primes: + counter += x +print(counter) diff --git a/primes/Cargo.toml b/primes/Cargo.toml new file mode 100755 index 0000000..164304b --- /dev/null +++ b/primes/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "primes" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/primes/src/main.rs b/primes/src/main.rs new file mode 100755 index 0000000..07586d8 --- /dev/null +++ b/primes/src/main.rs @@ -0,0 +1,94 @@ +use std::iter::Peekable; + +fn main() { +// +// println!("sum of primes less than 2,000,000 is : {}", +// PrimeItr::default() +// .take_while( |x| x < 2_000_000 ) +// .sum::()) +// +// println!("largest prime factor is : {}", PrimeItr::default() +// .take_while( |x| *x < 600851475143/2 ) +// .filter( |x| x % 600851475143 != 0 ) +// .last() +// .expect(PRIME_ERR) +// ) +// + for x in PrimeFactItr::factors(600851475143) { + println!("{}", x) + } +} + +const PRIME_ERR: &str = "unexpected item in bagging area"; + +//prime iterator + +struct PrimeItr { + record: Vec, + index: usize +} + +impl Iterator for PrimeItr { + type Item = usize; + fn next ( &mut self ) -> Option { + let is_prime = !self.record.iter() + .take_while( |x| **x * **x < self.index ) + .any( |x| self.index % *x == 0 ); + let result = self.index; + self.index += if self.index == 2 { 1 } else { 2 }; + if is_prime { + self.record.push(result); + Some(result) + } + else { + self.next() + } + } +} + +impl Default for PrimeItr { + fn default() -> Self { + PrimeItr { + record: vec![], + index: 2 + } + } +} + +// prime factorization iterator + +struct PrimeFactItr { + record: Vec, + chuck: usize, + prime: Peekable +} + +impl Iterator for PrimeFactItr { + type Item = usize; + fn next ( &mut self ) -> Option { + let x = *self.prime.peek().expect(PRIME_ERR); + let is_factor = self.chuck % x == 0; + if is_factor { + self.record.push(x); + self.chuck /= x; + Some(x) + } + else if x > self.chuck { + None + } + else { + self.prime.next(); + self.next() + } + } +} + +impl PrimeFactItr { + fn factors(x: usize) -> Self { + PrimeFactItr { + record: vec![], + chuck: x, + prime: PrimeItr::default().peekable() + } + } +}