|
|
|
|
|
by guccihat
512 days ago
|
|
The constraint only states not to use hard coded matrices as I read it. Here is a version with number addition removed, this works fully on strings. function isDivisibleByThree(num) {
let modulo, mod3 = "012012012012";
for (const digit of num) {
[modulo] = mod3.slice(modulo).slice(digit);
}
return modulo === "0";
}
|
|
But my real point is they did have the digit sum insight. Their code was already doing your previous suggestion, and if there's a compact way to slice in typescript types it could be adapted to this new method by replacing SUM_TABLE and changing one other line.
The only difference is that they're doing a sum modulo 9 instead of modulo 3, but both of those work fine.