Skip to content

Commit

Permalink
Try again on type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 23, 2024
1 parent ab9d754 commit fc1a543
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/rust/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
Returns the factorial of a given number. Note that it only accepts a ``u8`` because
any number that requires a larger type is *guaranteed* to overflow.

.. rust:fn:: math::n_choose_r<I: NumAssign + From<i8> + From<usize>>(n: usize, r: usize) -> I
.. rust:fn:: math::n_choose_r<I: NumAssign + PartialOrd>(n: u64, r: u64) -> I
Returns the number of ways to choose r items from a set of n.

Expand Down
16 changes: 8 additions & 8 deletions rust/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ pub fn factorial<I: NumAssign + From<u8>>(n: u8) -> I {
return answer
}

pub fn n_choose_r<I: NumAssign + From<i8> + From<u64> + PartialOrd>(n: u64, r: u64) -> I {
pub fn n_choose_r<I: NumAssign + PartialOrd>(n: u64, r: u64) -> I {
// slow path for larger numbers
let mut answer: I = one();
let sn: usize = n.into();
let sr: usize = r.into();
let sn: usize = n.try_into().unwrap();
let sr: usize = r.try_into().unwrap();
let mut tmp: I;
let mut factors: Vec<i8> = vec![0; n + 1];

Check failure

Code scanning / clippy

mismatched types Error

mismatched types
// collect factors of final number
Expand Down Expand Up @@ -45,25 +45,25 @@ pub fn n_choose_r<I: NumAssign + From<i8> + From<u64> + PartialOrd>(n: u64, r: u
while i <= sn {
while factors[i] > 0 {
tmp = answer;
answer *= i.into();
answer *= i.try_into().unwrap();

Check failure

Code scanning / clippy

the trait bound I: std::convert::TryFrom<usize> is not satisfied Error

the trait bound I: std::convert::TryFrom<usize> is not satisfied
while answer < tmp && j <= sn {
while factors[j] < 0 {
tmp /= j.into();
tmp /= j.try_into().unwrap();

Check failure

Code scanning / clippy

the trait bound I: std::convert::TryFrom<usize> is not satisfied Error

the trait bound I: std::convert::TryFrom<usize> is not satisfied
factors[j] += 1;
}
j += 1;
answer = tmp * i.into();
answer = tmp * i.try_into().unwrap();

Check failure

Code scanning / clippy

the trait bound I: std::convert::TryFrom<usize> is not satisfied Error

the trait bound I: std::convert::TryFrom<usize> is not satisfied
}
if answer < tmp {
return (-1).into(); // this indicates an overflow
panic!(); // overflow
}
factors[i] -= 1;
}
i += 1;
}
while j <= sn {
while factors[j] < 0 {
answer /= j.into();
answer /= j.try_into().unwrap();

Check failure

Code scanning / clippy

the trait bound I: std::convert::TryFrom<usize> is not satisfied Error

the trait bound I: std::convert::TryFrom<usize> is not satisfied
factors[j] += 1;
}
j += 1;
Expand Down

0 comments on commit fc1a543

Please sign in to comment.