你能用以下字符组成多少个可能的 URL?

发布于 2024-08-10 10:56:36 字数 84 浏览 8 评论 0原文

我想为 200 万个资产制作一个短 URL 服务,但我想使用尽可能短的字符数。

我需要使用什么数学方程来计算它?我知道这与阶乘有关,对吗?

I want to make a short URL service for 2 million assets but I want to use the shortest number of possible characters.

What is the math equation that I would need to use to figure it out? I know it has something to do with factorials, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鱼忆七猫命九 2024-08-17 10:56:36

这不是阶乘问题,而是指数问题。

如果x是可能的字符数,则需要对y求解以下方程:

x^y = 2000000

如果要使用所有数字和区分大小写的字母[0 -9A-Za-z],您有 62 个可能的值。这意味着您需要解决:

     62^y = 2000000
y*log(62) = log(2000000)
        y = log(2000000) / log(62)
        y = 3.5154313828...

当然,您的 URL 中不能有 3.5 个字符,因此您需要 4 个字符。如果您想更改 URL 使用的字符集,只需使用数字解决上述问题您的集合中的值。

注意 求解此方程假定 URL 长度固定。对于可变长度 URL,请参阅 Rob 的回答。

It's not a factorial problem, but an exponential one.

If x is the number of possible characters, you need to solve the following equation for y:

x^y = 2000000

If you want to use all numbers and case-sensitive alpha [0-9A-Za-z], you have 62 possible values. This means you need to solve:

     62^y = 2000000
y*log(62) = log(2000000)
        y = log(2000000) / log(62)
        y = 3.5154313828...

Of course, you can't have 3.5 characters in your URL, so you would need 4. If you want to change the character set you are using for your URL's, simply resolve the problem above using the number of values in your set.

Note Solving this equation assumes fixed-length URL's. For variable-length URL's, see Rob's answer.

蹲在坟头点根烟 2024-08-17 10:56:36

@jheddings 很接近,并且得到了正确的答案,但数学不太正确。不要忘记,您并不局限于特定长度的所有字符排列。您还可以利用长度为 1 到 y 个字符的 URL。因此我们想要这个和的闭合值:

x + x^2 + x^3 + ... + x^y = 2000000

幸运的是,这个和有一个闭合形式:

x + x^2 + x^3 + ... + x^y = x*(x^y - 1)/(x-1) = 2000000

x 是我们范围内可能的字符数。为了简单起见,我们假设它只包含小写字母、大写字母和数字 (26+26+10 = 62)。

然后我们得到以下等式:

2000000 = (62^(y+1) - 62)/(62-1)
2000000 = (62^(y+1) - 62)/(61)
2000000 * 61 = 62^(y+1) - 62
122000000 = 62^(y+1) - 62
122000000 + 62 = 62^(y+1)
122000062 = 62^(y+1)
log(122000062) = (y+1)
log(122000062) / log(62) = y+1
4.511492 = y+1
3.511492 = y

而且,正如您所说,3.5 个字符是不可能的,因此需要 4 个字符。诚然,在这种情况下差异并不重要。然而,在某些情况下(特别是在处理基数 2 时)它非常重要。

@jheddings is close, and got the right answer, but the math was not quite correct. Don't forget you are not limited to all the permutations of characters of a specific length. You can also leverage URLs of length 1 through y characters. Therefore we want the closed value of this sum:

x + x^2 + x^3 + ... + x^y = 2000000

Fortunately, there is a closed form for that sum:

x + x^2 + x^3 + ... + x^y = x*(x^y - 1)/(x-1) = 2000000

x is the number of possible characters in our range. For simplicity sake, let's assume it only includes lowercase, uppercase, and numbers (26+26+10 = 62.)

Then we get the following equation:

2000000 = (62^(y+1) - 62)/(62-1)
2000000 = (62^(y+1) - 62)/(61)
2000000 * 61 = 62^(y+1) - 62
122000000 = 62^(y+1) - 62
122000000 + 62 = 62^(y+1)
122000062 = 62^(y+1)
log(122000062) = (y+1)
log(122000062) / log(62) = y+1
4.511492 = y+1
3.511492 = y

And, as you said, 3.5 characters is impossible so 4 are required. Admittedly the difference doesn't matter in this case. However, in certain scenarios (especially when dealing with base 2) it is very important.

诗化ㄋ丶相逢 2024-08-17 10:56:36

可能的短 URL 数量 =(ID 中可能的不同字符数)的(url 中 ID 长度)次方

例如,如果您仅使用小写字符(其中有 26 个),并且您的 URL 看起来像http://domain.com/XXXXX (对于您的 5 个字符的唯一 ID),那么您可以制作 26 个^5 = 11,881,376 个短网址。

如果您使用大写和小写字母,则有 52 个,因此 52^5 = 380,204,032 个可能的短 URL,等等。

Number of possible short URLs = (Number of possible different characters in ID) raised to the power of (Length of ID in url)

For instance, if you're only using lowercase characters (of which there are 26) and your URLs look like http://domain.com/XXXXX (for your unique id's of 5 characters), then you can make 26^5 = 11,881,376 short urls.

If you were using upper and lower case letters, you'd have 52, so 52^5 = 380,204,032 possible short URLs, et cetera.

云裳 2024-08-17 10:56:36

您需要回答许多问题,例如您希望在集合中允许使用哪些类型的角色。

所有字母和所有数字? 36进制(5个字符可以容纳2mil+)

区分大小写?这会让你得到基数 62(4 个字符)

删除容易出错的字符和数字(例如 i/l 0/o)?大约以 32 为基数(也是 5 个字符)

You need to answer a number of questions, like what kinds of characters you want to allow in your set.

All letters and all digits? base 36 (5 characters can fit 2mil+)

Distinguish between upper and lowercase? That gets you to base 62 (4 characters)

Remove easily-mistaken characters and numbers (e.g. i/l 0/o)? roughly base 32 (also 5 characters)

久隐师 2024-08-17 10:56:36

您通常不需要任何数学魔法就可以解决此类问题。

26+26+10 = 62 个字符

Try 1. 62 = 62
Try 2. 62*62 = 3,844
Try 3. 62*62*62 = 238,328
Try 4. 62*62*62*62 = 14,776,336

所以 4 就是你的答案:)

You can often solve this kind of problem without any math wizardry.

26+26+10 = 62 characters

Try 1. 62 = 62
Try 2. 62*62 = 3,844
Try 3. 62*62*62 = 238,328
Try 4. 62*62*62*62 = 14,776,336

So 4 is your answer :)

妖妓 2024-08-17 10:56:36

根据 HTTP/URI 规范,您还可以使用以下“非保留字符”:ALPHA / DIGIT /“-”/“。” / "_" / "~"

这会在你的基数中添加额外的 4 个字符,因此

Math.log(2000000) / Math.log(66) = 3.4629721616408813

尽管这仍然意味着你最终会得到最多 4 个字符的 URL 路径。

According to the HTTP/URI Spec you can additionally use the following "unreserved characters": ALPHA / DIGIT / "-" / "." / "_" / "~"

That adds an additional 4 characters to your radix and thus

Math.log(2000000) / Math.log(66) = 3.4629721616408813

Although this still means you will end up with a 4 character URL path at maximum.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文