Compare commits

...

2 Commits

Author SHA1 Message Date
e3f9ab49f3 haskell: add p3
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-10-27 04:21:00 +05:30
4e98bf2115 rust: add p1, p2
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-10-27 04:16:43 +05:30
7 changed files with 48 additions and 5 deletions

View File

@@ -1,4 +1,13 @@
module Lib where
module Lib (fib, primeFactors) where
fib :: [ Integer ]
fib :: [Integer]
fib = 1 : 2 : zipWith (+) fib (tail fib)
primeFactors :: Integer -> [Integer]
primeFactors n = genPrimeFactors n 2
where
genPrimeFactors :: Integer -> Integer -> [Integer]
genPrimeFactors n c
| c == n = [n]
| mod n c /= 0 = genPrimeFactors n (c + 1)
| otherwise = c : genPrimeFactors (div n c) c

7
haskell/p3.hs Normal file
View File

@@ -0,0 +1,7 @@
import Lib (primeFactors)
main :: IO ()
main = putStr $ show solve
solve :: Integer
solve = last $ primeFactors 600851475143

View File

@@ -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"

View File

@@ -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##*/}

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