JSFiddle - React, Tailwind, and code Playground
Testing digit frequencies with Modulo operations
by skibulk
JavaScript
console.clear();
var min = 1e14;
var max = 1e14 + 1000000;
var mod = 10000000000000;
var results = [];
// DEMO 1 -------------
for(var i = min; i <= max; i++){
var digits = (i % mod + "").split("");
// Iterate number positions
for(var j = 0; j < digits.length; j++){
var pos = digits.length - 1 - j;
if(results[pos] == undefined){
results[pos] = new Array(10).fill(0);
}
results[pos][digits[j]]++;
}
}
console.log(results);
// DEMO 2 -------------
/*
var out = "A";
for(var i = min; i <= max; i++){
var tmp = i % mod;
out += hash(tmp + "");
}
for(var j = 0; j <= 9; j++){
results[j] = out.match(new RegExp(j, "g")).length;
}
console.log(results);
// https://stackoverflow.com/a/8831937/6465140
function hash(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
var char = str.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash;
}
return hash;
}
*/