do not use %, in favour of division, multiplication and addition
parent
8d544f276e
commit
18c58c7244
|
|
@ -68,15 +68,21 @@ pub fn digits(num i64, params DigitParams) []int {
|
||||||
sign = -1
|
sign = -1
|
||||||
n = -n
|
n = -n
|
||||||
}
|
}
|
||||||
|
|
||||||
mut res := []int{}
|
mut res := []int{}
|
||||||
// if number passed to function is 0 then short-circuit and return 0
|
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
|
// short-circuit and return 0
|
||||||
res << 0
|
res << 0
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
for n != 0 {
|
for n != 0 {
|
||||||
res << int((n % b) * sign)
|
next_n := n / b
|
||||||
n /= b
|
res << int(n - next_n * b)
|
||||||
|
n = next_n
|
||||||
|
}
|
||||||
|
|
||||||
|
if sign == -1 {
|
||||||
|
res[res.len - 1] *= sign
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.reverse {
|
if params.reverse {
|
||||||
|
|
@ -88,8 +94,17 @@ pub fn digits(num i64, params DigitParams) []int {
|
||||||
|
|
||||||
// count_digits return the number of digits in the number passed.
|
// count_digits return the number of digits in the number passed.
|
||||||
// Number argument accepts any integer type (i8|i16|int|isize|i64) and will be cast to i64
|
// Number argument accepts any integer type (i8|i16|int|isize|i64) and will be cast to i64
|
||||||
pub fn count_digits(n i64) int {
|
pub fn count_digits(number i64) int {
|
||||||
return int(ceil(log(abs(f64(n) + 1)) / log(10.0)))
|
mut n := number
|
||||||
|
if n == 0 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
mut c := 0
|
||||||
|
for n != 0 {
|
||||||
|
n = n / 10
|
||||||
|
c++
|
||||||
|
}
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// minmax returns the minimum and maximum value of the two provided.
|
// minmax returns the minimum and maximum value of the two provided.
|
||||||
|
|
|
||||||
|
|
@ -930,25 +930,24 @@ fn test_digits() {
|
||||||
assert digits(100, base: 128, reverse: true) == [100]
|
assert digits(100, base: 128, reverse: true) == [100]
|
||||||
assert digits(100, base: 256, reverse: true) == [100]
|
assert digits(100, base: 256, reverse: true) == [100]
|
||||||
|
|
||||||
palindrom_digits_in_10th_base := digits(i64(1234432112344321))
|
assert digits(1234432112344321) == digits(1234432112344321, reverse: true)
|
||||||
assert palindrom_digits_in_10th_base == [1, 2, 3, 4, 4, 3, 2, 1, 1, 2, 3, 4, 4, 3, 2, 1]
|
assert digits(1234432112344321) == [1, 2, 3, 4, 4, 3, 2, 1, 1, 2, 3, 4, 4, 3, 2, 1]
|
||||||
|
|
||||||
assert digits(125, base: 10, reverse: true) == [1, 2, 5]
|
assert digits(125, base: 10, reverse: true) == [1, 2, 5]
|
||||||
|
assert digits(125, base: 10).reverse() == [1, 2, 5]
|
||||||
|
|
||||||
digits_in_10th_base := digits(125, base: 10).reverse()
|
assert digits(15, base: 16, reverse: true) == [15]
|
||||||
assert digits_in_10th_base == [1, 2, 5]
|
assert digits(127, base: 16, reverse: true) == [7, 15]
|
||||||
|
assert digits(65535, base: 16, reverse: true) == [15, 15, 15, 15]
|
||||||
|
assert digits(-65535, base: 16, reverse: true) == [-15, 15, 15, 15]
|
||||||
|
|
||||||
digits_in_16th_base := digits(15, base: 16).reverse()
|
assert digits(-127) == [7, 2, -1]
|
||||||
assert digits_in_16th_base == [15]
|
assert digits(-127).reverse() == [-1, 2, 7]
|
||||||
|
assert digits(-127, reverse: true) == [-1, 2, 7]
|
||||||
|
|
||||||
negative_digits := digits(-4, base: 2).reverse()
|
assert digits(234, base: 7).reverse() == [4, 5, 3]
|
||||||
assert negative_digits == [-1, 0, 0]
|
|
||||||
|
|
||||||
digits_in_7th_base := digits(234, base: 7).reverse()
|
assert digits(67432, base: 12).reverse() == [3, 3, 0, 3, 4]
|
||||||
assert digits_in_7th_base == [4, 5, 3]
|
|
||||||
|
|
||||||
digits_in_12th_base := digits(67432, base: 12).reverse()
|
|
||||||
assert digits_in_12th_base == [3, 3, 0, 3, 4]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that math functions of high angle values
|
// Check that math functions of high angle values
|
||||||
|
|
@ -995,7 +994,19 @@ fn test_powi() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_count_digits() {
|
fn test_count_digits() {
|
||||||
|
assert count_digits(-999) == 3
|
||||||
|
assert count_digits(-100) == 3
|
||||||
|
assert count_digits(-99) == 2
|
||||||
|
assert count_digits(-10) == 2
|
||||||
|
assert count_digits(-1) == 1
|
||||||
|
assert count_digits(0) == 1
|
||||||
|
assert count_digits(1) == 1
|
||||||
|
assert count_digits(10) == 2
|
||||||
|
assert count_digits(99) == 2
|
||||||
|
assert count_digits(100) == 3
|
||||||
|
assert count_digits(999) == 3
|
||||||
|
//
|
||||||
assert count_digits(12345) == 5
|
assert count_digits(12345) == 5
|
||||||
assert count_digits(i64(1234567890)) == 10
|
assert count_digits(123456789012345) == 15
|
||||||
assert count_digits(-67345) == 5
|
assert count_digits(-67345) == 5
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue