Skip to content

Commit

Permalink
Merge pull request #5 from DanArmor/main
Browse files Browse the repository at this point in the history
fix: return 1 for gcd_for_slice if all elements are 0.0
  • Loading branch information
ichi-h authored Oct 1, 2023
2 parents 2f03855 + 123f4e9 commit 44feed1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ pub mod math {
}

let first = slice[0];
slice.iter().fold(
let mut iter = slice.iter().skip_while(|x| x == &&0);
let first = match iter.next() {
Some(v) => *v,
None => return 1
};

let gcd = iter.fold(
first,
|acc, cur| {
if *cur == 0 {
Expand All @@ -24,7 +30,8 @@ pub mod math {
gcd(*cur, acc)
}
},
)
);
gcd
}
}

Expand Down

0 comments on commit 44feed1

Please sign in to comment.