File tree Expand file tree Collapse file tree 1 file changed +13
-14
lines changed Expand file tree Collapse file tree 1 file changed +13
-14
lines changed Original file line number Diff line number Diff line change 1
1
use num_bigint:: BigUint ;
2
+ use num_traits:: Zero ;
2
3
3
4
/// Calculates the k-adicity of n, i.e., the number of trailing 0s in a base-k
4
5
/// representation.
5
6
pub const fn k_adicity ( k : u64 , mut n : u64 ) -> u32 {
7
+ if n == 0 {
8
+ return 0 ;
9
+ }
6
10
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;
14
14
}
15
15
r
16
16
}
17
17
18
18
/// Calculates the k-adicity of n, i.e., the number of trailing 0s in a base-k
19
19
/// representation.
20
20
pub fn k_adicity_big_int ( k : BigUint , mut n : BigUint ) -> u32 {
21
+ if n. is_zero ( ) {
22
+ return 0 ;
23
+ }
21
24
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;
29
28
}
30
29
r
31
30
}
You can’t perform that action at this time.
0 commit comments