rust: add p1, p2

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-10-27 04:16:43 +05:30
parent 6c89f18e9d
commit 4e98bf2115
5 changed files with 30 additions and 3 deletions

View File

@@ -2,3 +2,11 @@
name = "eulerfunt" name = "eulerfunt"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[[bin]]
name = "p1"
path = "./src/p1.rs"
[[bin]]
name = "p2"
path = "./src/p2.rs"

View File

@@ -10,9 +10,7 @@ version = "0.1.0"
edition = "2021" edition = "2021"
EOF EOF
if ! [ -e ./src/p*.rs ]; then [[ ! $(ls ./src/p*.rs) ]] && exit 0
exit 0
fi
for problem in ./src/p*.rs; do for problem in ./src/p*.rs; do
file=${problem##*/} file=${problem##*/}

View File

@@ -0,0 +1,11 @@
pub fn fib_till(lim: u32) -> Vec<u32> {
let mut ret: Vec<u32> = vec![1, 2];
let mut n: usize = 2;
while ret[n - 1] <= lim {
ret.push(ret[n - 1] + ret[n - 2]);
n += 1;
}
ret
}

4
rust/src/p1.rs Normal file
View File

@@ -0,0 +1,4 @@
fn main() {
let multiple_sum = |x| (x..1000).step_by(x).into_iter().sum::<usize>();
print!("{}", multiple_sum(3) + multiple_sum(5) - multiple_sum(15));
}

6
rust/src/p2.rs Normal file
View File

@@ -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::<u32>());
}