Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
Created April 24, 2022 06:01
Show Gist options
  • Select an option

  • Save MikuroXina/811bec321ddf5cdf79973f7118cf1973 to your computer and use it in GitHub Desktop.

Select an option

Save MikuroXina/811bec321ddf5cdf79973f7118cf1973 to your computer and use it in GitHub Desktop.
Divisors enumerator
pub fn divisors(with: u64) -> Vec<u64> {
assert_ne!(with, 0, "cannot find divisors with 0");
let at_most = ((with as f64).sqrt() * 2.0).floor() as usize;
let mut divs = Vec::with_capacity(at_most);
for i in (1..)
.filter(|&i| with % i == 0)
.take_while(|&i| i * i <= with)
{
divs.push(i);
if i * i != with {
divs.push(with / i);
}
}
divs.sort_unstable();
divs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment