Skip to content

Commit c28210b

Browse files
committed
optim: k-adicity utility
1 parent 2201c8a commit c28210b

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

ff/src/fields/utils.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
use num_bigint::BigUint;
2+
use num_traits::Zero;
23

34
/// Calculates the k-adicity of n, i.e., the number of trailing 0s in a base-k
45
/// representation.
56
pub const fn k_adicity(k: u64, mut n: u64) -> u32 {
7+
if n == 0 {
8+
return 0;
9+
}
610
let mut r = 0;
7-
while n > 1 {
8-
if n % k == 0 {
9-
r += 1;
10-
n /= k;
11-
} else {
12-
return r;
13-
}
11+
while n % k == 0 {
12+
r += 1;
13+
n /= k;
1414
}
1515
r
1616
}
1717

1818
/// Calculates the k-adicity of n, i.e., the number of trailing 0s in a base-k
1919
/// representation.
2020
pub fn k_adicity_big_int(k: BigUint, mut n: BigUint) -> u32 {
21+
if n.is_zero() {
22+
return 0;
23+
}
2124
let mut r = 0;
22-
while &n > &1_u8.into() {
23-
if &n % &k == 0_u8.into() {
24-
r += 1;
25-
n /= &k;
26-
} else {
27-
return r;
28-
}
25+
while (&n % &k).is_zero() {
26+
r += 1;
27+
n /= &k;
2928
}
3029
r
3130
}

0 commit comments

Comments
 (0)