I was working on a program and its job is to iterate through all possible ASCII characters. The code is able to successfully convert up to a point. When the number100000000000000008191
is entered the returned value is !__Ej2~nHK%
when the next number 100000000000000008192
is entered the return value is !__Ej2~nHK%
, the same value. This occurs for the next in the sequence and the next. It becomes so ineffective that 16384 numbers generate the same values.
This shouldn't happen as when counting in any number system different value numbers should not be the same.
Does anyone know what is wrong with it?
function generate(state) {
const printables = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
var ones = state % printables.length
var newstate = Math.floor(state / printables.length)
if (newstate !== 0) {
return generate(newstate) + printables[ones]
} else {
return printables[ones]
}
}
console.log(generate(90071992514740992227))