# get individual digits of

## Description

"what are the decimal digits of 𝑛 ∈ ℕ" "figure it out with Math.ceil(Math.log(𝑛)) or String(𝑛).length" "which is...... faster" c.f. https://jsperf.app/gorehi for just getting the length itself

## Setup

``````// only works if JS doesn't sciencify the string
// representation for large values (X.XXXe+X)
// so like... 𝑛 < 10²¹ or so?
const L = 10e20;
function test (digitsOfN) {
let 𝑛 = 100;
while ((𝑛 *= 1.19) < L) digitsOfN(𝑛);
}``````

## Test runner

Testing in
TestOps/sec
get the digits of 𝑛 using 𝕸𝖆𝖙𝖍 ①
``````// goes left to right shaving off the leading digit
test(n => {
let l = Math.ceil(Math.log10(n))
const d = [];
while (l--) {
const m = (10**l);
d.push(Math.trunc(n / m))
n = n % m;
}
return d;
});
``````
get the digits of 𝑛 using 𝓢𝓽𝓻𝓲𝓷𝓰
``test(n => Array.from(String(n), Number));``
get the digits of 𝑛 using 𝕸𝖆𝖙𝖍 ②
``````// goes right to left shaving off the last digit
test(n => {
const d = [];
while (n) {
const l = n % 10;
n = (n - l) / 10
d.unshift(l);
}
return d;
});``````
get the digits of 𝑛 using 𝕸𝖆𝖙𝖍 ③
``````// goes right to left shaving off the last digit
// (assembles in reverse then flips the array)
test(n => {
const d = [];
while (n) {
const l = n % 10;
n = (n - l) / 10
d.push(l);
}
return d.reverse();
});``````
get the digits of 𝑛 using 𝕸𝖆𝖙𝖍 ④
``````// goes right to left shaving off the last digit
// (trying optimizations)
test(n => {
const d = [];
while (n) {
d.unshift(n % 10);
n = Math.trunc(n / 10)

}
return d;
});``````
get the digits of 𝑛 using 𝕸𝖆𝖙𝖍 ⑤
``````// goes right to left shaving off the last digit
// (assembles in reverse then flips the array)
// (trying optimizations)
test(n => {
const d = [];
while (n) {
d.push(n % 10);
n = Math.trunc(n / 10)
}
return d.reverse();
});
``````