Xnary(如二进制,但不同的)计数 [英] Xnary (like binary but different) counting

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

问题描述

我在做一个功能,可将数字转换成字符串,predefined字符。原来,我知道。我开始了它,因为它似乎乐趣的时间。这样做我自己。好吧,这是令人沮丧和不好玩。

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.

我希望它像二进制文件作为任何左边的字符是价值超过其右侧neigbour。二进制是低效的,因为每一个位只有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??

我发现 <一个href="http://stackoverflow.com/questions/4273211/function-for-writing-out-a-base-7-word-like-binary-counter-style">here,但 AA 是一样的 A AAA 。该函数的结果是永远 AA AAA

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

字符串 A 不同于 AA AAA 然而,这样的数目应该太。 (与二进制 1 01 001 等)而且,由于更长的字符串总是比一个短... A&LT更有价值; AA&LT; 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&LT; AA&LT; AAA ,我的农行的值比其他脚本的价值更高。另一个区别:我的剧本是不存在的,因为我一直在失败

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

如果 1 A (在第一个的xnary的),什么是 0 ?不可能是它是什么。

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

所以,这个人是一个半身像。另一种(上的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的列编号。它更容易,如果我们转向一个, A = 0,...,Z = 25,AA = 26,... ,至少计算。对于你的计划,所有的需要则是1转换到Xnary RESP之前减法。另外一个从转换后。

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.

所以,与修改,让我们开始寻找的转换。首先,有多少符号,我们需要连接code N ?嗯,有26个位数的数字,26 ^ 2双位数字,26 ^ 3三位数字等,因此,使用最多 D 位数字的总是 26 ^ 1 + 26 ^ 2 + ... + 26 ^ D 。这是一个几何级数的开始,我们知道一个封闭形式的总和, 26 *(26 ^ D - 1)/(26-1)。因此,要EN code N ,我们需要 D 的数字,如果

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)=地板(log_26(25 * N / 26 + 1))+ 1 数字为en code N'GT; = 0 。现在,我们必须减去需要至多 D数字的总(N) - 1 数字找到的位置 N D(N)位数字号码,让我们把它叫做 P(N)= N - 26 *(26 ^(D(N) - 1)-1)/ 25 。和编码 N 是那么简单的一个 D(N)的位数字基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).

在其他方向上的转化是然后碱-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 ,我们EN code N = 999 log_26(25 *二十六分之九百九十九+ 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 = 158760 N = 158759 log_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天全站免登陆