Search code examples
phpmathencryptionformularadix

Algorithm to transform number into a string, based on available alphabet and considering minimum string length


I'm trying to figure out how to transform a number, into a string based on an available alphabet, and also considering a minimum string length.

For example:

Considering I have this alphabet: AB12 And that I define the minimum string length to be: 2

The number 1 would be = AA
The number 2 would be = AB
The number 3 would be = A1
The number 4 would be = A2
The number 5 would be = BA
The number 6 would be = BB
The number 7 would be = B1
The number 8 would be = B2
The number 9 would be = 1A
The number 10 would be = 1B
...
if I run out of combinations, string length should increase:
The number 16 would be = 22
The number 17 would be = AAA
The number 18 would be = AAB
...

How can I define a formula for this ? ( I will implement this formula with PHP programming language but I guess this is math right ? How can I create a 'formula' for this ? Can you help me get there?

Will it always cost a recursive iteration ? Is there a way to quickly get to the result ?

Lets say my number is at 400000 or 1 000 000, will it always cost alot of iteration to get to the result? Is there a way to avoid the iteration/cost?

EDIT:

I loved @Stef's answer, very very interesting.. It is almost perfect!!! ( actually it can work for me ! so It can actually be called perfect )

But...

I've been comparing the results of the base_convert results with my alphabet, with my database seeder results,

sample code for my output below ( in case anyone is interested .. )

for ($i = 0; $i < 1332; $i++){
    $alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789';
    $totalAlphabetChars = strlen($alphabet);
    echo base_convert($i, 10, $totalAlphabetChars) . PHP_EOL;
}

and I've found out a difference! hehe...

the base_convert has less 36 combinations ( at least when we have a string length 2, most likely this loss of possibilities/combinations is increased as string length increases )

and since we are here, I think it is worth asking/sharing..

let's imagine I want to fulfil some ambitious desires...

check the differences below:

base_convert no zeros to the left

with my recursive functions / seeder, I can get the combinations that consider zeros to the left, but base_convert does not give me combinations with 0 to the left, ideally for my perfect world, I want the most amount of possibilities, considering 0s to the left also. For example / meaning:

Line 37 should give me 00 instead of 10
Line 38 should give me 01 instead of 11
..
Line 72 should give me 0z instead of 1z

Is there a way of achieving this with base_convert style ? ( no iterations or recursive functions, just some kind of 'instant' conversion/result like we have at this current state of this question/answer/edit?

Another Edit:

Below the amount of possibilities/combinations in 100.000, one using recursive functions ( that include 0 on the left ): possibilities/combinations in 100k using recursive

and this one using base_convert based on @Stef's answer ( not including combinations/possibilities with zeros on the left ) enter image description here

I can definitely use base_convert and accept the loss of the combinations with zeros on the left, but it is possible to have them I want to know ^^

Thankss alot!!


Solution

  • You have an alphabet containing k symbols, and you want to make n distinct combinations of these symbols.

    Map your k symbols to digits 0, 1, 2, ..., k-1. Then your problem is as easy as rewriting a number into base-k.

    In PHP you can use function base_convert to convert a number from base 10 to base k, then function strtr to replace the standard digits of base k into your custom symbols.

    To convert 400000 into your base AB12:

    $s = base_convert("400000", 10, 4)
    $s = strtr($s, "0123", "AB12")
    
    echo $s