A package for integer digit computations.
Based on julias digits()
function.
You can install the package by typing:
Pkg.add("Digits")
using Digits
or clone the latest version directly from the repo:
Pkg.clone("https://github.com/greenflash1357/Digits.jl.git")
using Digits
Returns the digits of n in reversed order, i.e. reversedigits(n) == undigit(reverse!(digits(n)))
. For a list of digits this is equivalent to reverse()
and reverse!()
.
The inverse function to julias digits()
. Accepting a vector of integers where more significant digits are at higher indexes, according to digits()
and a base. Default is 10.
Creates a digit histogram for a given number. The result is a 1-dimensional 10-element array containing the count of each digit. The first element contains the count of 0, while the last represents the count of 9.
Checks whether a or b is part of the other, e.g. contains(1356,35)
will result in true
.
Checks wether a or b is the first/last part of the other, e.g. startswith(1356,1)
, startswith(32,3236)
, endswith(31,9831)
will all return true
.
Returns true if a and b are valid permutations of their digits.
Returns true if n is a palindromic number.
Cuts off i digits from n. If i is positive the first (most significant) digits are cropped. If i is negative the last digits are cut off.
Return the combination of a and b, similar to a string concatenation, e.g. combine(13,56)
-> 1356
.
Calculates the cross sum over n.
Returns the digits of n with index idx. idx can be an array or range, e.g. select(64247,[2:4])
-> 424
Replaces the digits of n with index idx by the values in vals. vals has to be same size as idx.
For arrays of digits there is also an inplace version of replace.
Example: replace(6215,[2,4],[8,7])
-> 6817
Replaces the all digits of n with value olddigit with newdigit. There is also an inplace operation for arrays of digits.
Example: replace(1363,3,9)
-> 1969
Computes the digital root of n. That is the iterative sum of the digits until the result is a single digit number. digitroot(13548)
-> 3
- Most functions accept an array of digits aswell as an integer as input.
- For most of the array methods there exists an inplace operation.
- This package should also work for negative integers, but this can become tricky. Take a closer look at the digit representation of negative integers.
digits(-135)
->[-5, -3, -1]
.