T
T
t38c3j2016-10-10 22:35:05
PHP
t38c3j, 2016-10-10 22:35:05

Is there a formula for this?

Hello. There is not a big problem from the main task that I solved, but I would like it to be more elegant.
There is a function that receives ID and string length as input, which, depending on the ID, returns a string of characters of a certain length.
For example: we use only az and the length of the string should be 3 characters, that is, we have 26*26*26=17576 unique strings.
For example, if you submit ID 2, then it is 1,1,2 or, for example, ID 555, then it is 1,23,9 (1,23,9 are serial numbers of letters).
I do it all by recursion, is there a mathematical formula so that I can calculate the ordinal numbers of letters based on the ID?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2016-10-10
@t38c3j

This is called "translation to 26-ary number system".
The classic algorithm: in the loop, while the number is greater than 0, divide by 26, the remainder is the serial number of the next letter, and the quotient is fed to the input of the next pass of the loop.
There is no universal formula, because it will use remainders from powers of 26, and will be slower than the algorithm described above.

A
Alexander Aksentiev, 2016-10-10
@Sanasol

hashids.org/php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question