From 4e98bf2115bc95870dafb50f0cf24a73b82142a6 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Thu, 27 Oct 2022 04:16:43 +0530 Subject: [PATCH] rust: add p1, p2 Signed-off-by: Amneesh Singh --- rust/Cargo.toml | 8 ++++++++ rust/gentoml | 4 +--- rust/src/lib.rs | 11 +++++++++++ rust/src/p1.rs | 4 ++++ rust/src/p2.rs | 6 ++++++ 5 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 rust/src/p1.rs create mode 100644 rust/src/p2.rs diff --git a/rust/Cargo.toml b/rust/Cargo.toml index d22155a..e07a449 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -2,3 +2,11 @@ name = "eulerfunt" version = "0.1.0" edition = "2021" + +[[bin]] +name = "p1" +path = "./src/p1.rs" + +[[bin]] +name = "p2" +path = "./src/p2.rs" diff --git a/rust/gentoml b/rust/gentoml index 951a0d8..221a055 100755 --- a/rust/gentoml +++ b/rust/gentoml @@ -10,9 +10,7 @@ version = "0.1.0" edition = "2021" EOF -if ! [ -e ./src/p*.rs ]; then - exit 0 -fi +[[ ! $(ls ./src/p*.rs) ]] && exit 0 for problem in ./src/p*.rs; do file=${problem##*/} diff --git a/rust/src/lib.rs b/rust/src/lib.rs index e69de29..6c90bcc 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -0,0 +1,11 @@ +pub fn fib_till(lim: u32) -> Vec { + let mut ret: Vec = vec![1, 2]; + let mut n: usize = 2; + + while ret[n - 1] <= lim { + ret.push(ret[n - 1] + ret[n - 2]); + n += 1; + } + + ret +} diff --git a/rust/src/p1.rs b/rust/src/p1.rs new file mode 100644 index 0000000..f2b18de --- /dev/null +++ b/rust/src/p1.rs @@ -0,0 +1,4 @@ +fn main() { + let multiple_sum = |x| (x..1000).step_by(x).into_iter().sum::(); + print!("{}", multiple_sum(3) + multiple_sum(5) - multiple_sum(15)); +} diff --git a/rust/src/p2.rs b/rust/src/p2.rs new file mode 100644 index 0000000..9cd020a --- /dev/null +++ b/rust/src/p2.rs @@ -0,0 +1,6 @@ +use eulerfunt::fib_till; + +fn main() { + let evenfib = fib_till(4000000).into_iter().filter(|x| x % 2 == 0); + print!("{}", evenfib.sum::()); +}