Xnary(类似于二进制但不同)计数 [英] Xnary (like binary but different) counting

查看:11
本文介绍了Xnary(类似于二进制但不同)计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个函数,将数字转换为带有预定义字符的字符串.原创,我知道.我开始了它,因为它当时看起来很有趣.自己来做.嗯,这很令人沮丧,而且不好玩.

I'm making a function that converts a number into a string with predefined characters. Original, I know. I started it, because it seemed fun at the time. To do on my own. Well, it's frustrating and not fun.

我希望它像二进制一样,因为任何左边的字符都比它的右边的邻居更有价值.二进制是低效的,因为每个位只有 1 个正值.Xnary 是高效的,因为位"永远不会是 0.

I want it to be like binary as in that any left character is worth more than its right neigbour. Binary is inefficient because every bit has only 1 positive value. Xnary is efficient, because a 'bit' is never 0.

字符集(在本例中):A - Z.

The character set (in this case): A - Z.

A = 1 ..
Z = 26
AA = 27 ..
AZ = 52
BA = 53 ..
BZ = 2 * 26 (B) + 26 * 1 (Z) = 78... Right?
ZZ = 26 * 26 (Z) + 26 * 1 (Z) = 702?? Right??

我发现 这个 这里,但是那里 AA 和 <代码>A 和 AAA.函数的结果永远不会是 AAAAA.

I found this here, but there AA is the same as A and AAA. The result of the function is never AA or AAA.

字符串 AAAAAA 不同,所以数字也应该不同.(与二进制 101001 等不同.)而且由于较长的字符串总是比较短的字符串更有价值... A<AA<AAA.

The string A is different from AA and AAA however, so the number should be too. (Unlike binary 1, 01, 001 etc.) And since a longer string is always more valuable than a shorter... A < AA < AAA.

这有意义吗?我之前尝试过解释,但失败了.我以前也尝试过.=)

Does this make sense? I've tried to explain it before and have failed. I've also tried to make it before. =)

最重要的一点:因为 A <AA<AAA,'my' ABC 的值高于其他脚本的值.另一个区别:我的脚本不存在,因为我一直失败.

The most important thing: since A < AA < AAA, the value of 'my' ABC is higher than the value of the other script. Another difference: my script doesn't exist, because I keep failing.

我已经尝试过 这个算法:

N = 1000, Size = 3, (because 26 log(1000) = 2.x), so use 676, 26 and 1 for positions:
N = 1000
P0 = 1000 / 676 = 1.x = 1 = A
N = 1000 - 1 * 676 = 324
P1 = 324 / 26 = 12.x = 12 = L
N = 324 - 12 * 26 = 12
P1 = 12 / 1 = 12 = L
1000 => ALL

听起来很公平?显然是废话.因为:

Sounds fair? Apparently it's crap. Because:

N = 158760, Size = 4, so use 17576, 676, 26 and 1
P0 = 158760 / 17576 = 9.x = 9 = I
N = 158760 - 9 * 17576 = 576
P1 = 576 / 676 = 0.x = 0 <<< OOPS

如果 1A(xnary 的第一个),那么 0 是什么?不可能就是这样.

If 1 is A (the very first of the xnary), what's 0? Impossible is what it is.

所以这个是半身像.另一个(on jsFiddle)也是半身像,因为 A !=AA != AAA 这是事实.

So this one is a bust. The other one (on jsFiddle) is also a bust, because A != AA != AAA and that's a fact.

那么我在漫长的几个夜晚都错过了什么?

So what have I been missing for a few long nights?

哦,顺便说一句:如果您不喜欢数字,请不要阅读此内容.

Oh BTW: if you don't like numbers, don't read this.

PS.我试过搜索类似的问题,但没有一个足够相似.一个引用最相似,但有缺陷"的 IMO.

PS. I've tried searching for similar questions but none are similar enough. The one references is most similar, but 'faulty' IMO.

推荐答案

也称为 Excel 列编号.如果我们移动 1 会更容易,A = 0, ..., Z = 25, AA = 26, ...,至少对于计算而言.对于您的方案,在转换为 Xnary 之前,只需减去 1.转换后的加法.

Also known as Excel column numbering. It's easier if we shift by one, A = 0, ..., Z = 25, AA = 26, ..., at least for the calculations. For your scheme, all that's needed then is a subtraction of 1 before converting to Xnary resp. an addition after converting from.

因此,通过该修改,让我们开始寻找转换.首先,我们需要多少个符号来编码 n?嗯,有 26 个一位数,26^2 个两位数,26^3 个三位数等等.所以最多使用 d 位的数字的总数是 26^1 + 26^2 + ... + 26^d.这是几何级数的开始,我们知道和的封闭形式,26*(26^d - 1)/(26-1).所以要编码 n,我们需要 d 个数字 if

So, with that modification, let's start finding the conversion. First, how many symbols do we need to encode n? Well, there are 26 one-digit numbers, 26^2 two-digit numbers, 26^3 three-digit numbers etc. So the total of numbers using at most d digits is 26^1 + 26^2 + ... + 26^d. That is the start of a geometric series, we know a closed form for the sum, 26*(26^d - 1)/(26-1). So to encode n, we need d digits if

26*(26^(d-1)-1)/25 <= n < 26*(26^d-1)/25   // remember, A = 0 takes one 'digit'

26^(d-1) <= (25*n)/26 + 1 < 26^d

也就是说,我们需要 d(n) = floor(log_26(25*n/26+1)) + 1 个数字来编码 n >= 0.现在我们必须减去最多需要 d(n) - 1 个数字的数字的总和,以找到 nd(n)-数字,我们称之为p(n) = n - 26*(26^(d(n)-1)-1)/25.那么n的编码就是d(n)-digit base-26编码p(n).

That is, we need d(n) = floor(log_26(25*n/26+1)) + 1 digits to encode n >= 0. Now we must subtract the total of numbers needing at most d(n) - 1 digits to find the position of n in the d(n)-digit numbers, let's call it p(n) = n - 26*(26^(d(n)-1)-1)/25. And the encoding of n is then simply a d(n)-digit base-26 encoding of p(n).

另一个方向的转换是base-26扩展,然后是26*(26^(d-1) - 1)/25.

The conversion in the other direction is then a base-26 expansion followed by an addition of 26*(26^(d-1) - 1)/25.

所以对于 N = 1000,我们编码 n = 999, log_26(25*999/26+1) = log_26(961.5769...)= 2.x,我们需要 3 位数字.

So for N = 1000, we encode n = 999, log_26(25*999/26+1) = log_26(961.5769...) = 2.x, we need 3 digits.

p(999) = 999 - 702 = 297
297 = 0*26^2 + 11*26 + 11
999 = ALL

对于 N = 158760n = 158759log_26(25*158759/26+1) = 3.66...,我们需要四位数

For N = 158760, n = 158759 and log_26(25*158759/26+1) = 3.66..., we need four digits

p(158759) = 158759 - 18278 = 140481
140481 = 7*26^3 + 25*26^2 + 21*26 + 3
158759 = H        Z         V       D

这篇关于Xnary(类似于二进制但不同)计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆