iter-prod
cargo devel CI / cargo CI (push) Has been cancelled
Details
cargo devel CI / cargo CI (push) Has been cancelled
Details
This commit is contained in:
parent
22505d6d96
commit
3e27923d14
|
@ -2724,6 +2724,10 @@ version = "1.70.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
|
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "iter-prod"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.10.5"
|
version = "0.10.5"
|
||||||
|
|
|
@ -30,9 +30,11 @@ members = [
|
||||||
"members/arithmetics",
|
"members/arithmetics",
|
||||||
"members/egui-demo",
|
"members/egui-demo",
|
||||||
"members/rfd-demo",
|
"members/rfd-demo",
|
||||||
|
"members/iter-prod",
|
||||||
]
|
]
|
||||||
default-members = [
|
default-members = [
|
||||||
".",
|
".",
|
||||||
|
"members/iter-prod",
|
||||||
"members/rfd-demo",
|
"members/rfd-demo",
|
||||||
"members/egui-demo",
|
"members/egui-demo",
|
||||||
"members/arithmetics",
|
"members/arithmetics",
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "iter-prod"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,35 @@
|
||||||
|
fn main() {
|
||||||
|
let x = 7;
|
||||||
|
println!("{}", factorial(x));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn factorial(n: u128) -> u128 {
|
||||||
|
(1..n + 1).product()
|
||||||
|
}
|
||||||
|
|
||||||
|
// does the same as product from the stdlib
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn myprod<T>(iter: T) -> u128
|
||||||
|
where
|
||||||
|
T: Iterator<Item = u128>,
|
||||||
|
{
|
||||||
|
#[allow(clippy::unnecessary_fold)] // clippy detects that this is just Iter::product
|
||||||
|
iter.fold(1, |acc, v| acc * v)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn works() {
|
||||||
|
assert_eq!(factorial(7), 5040);
|
||||||
|
assert_eq!(factorial(9), 362880);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn myprod_same_as_real() {
|
||||||
|
assert_eq!((1..5).product::<u128>(), myprod(1..5));
|
||||||
|
assert_eq!((1..30).product::<u128>(), myprod(1..30));
|
||||||
|
// assert_eq!((1..50).product::<u128>(), myprod(1..50)); // u128 overflows here
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue