【问题标题】:Decimal expansion based on slot length summation基于槽长度求和的十进制扩展
【发布时间】:2014-12-26 12:38:05
【问题描述】:

我正在尝试创建一种算法来以某种方式生成十进制数。

a) 我有一个初始号码,比如i = 2.

b) 然后我有一个增量添加方法,比如f(n) { n * 2 }

c) 然后我有一个数字槽长度,比如l = 2,它为小数字创建前零并限制较长数字的最大长度。 2 变为 026464,但 512 = (5)12 其中 5 在前一个插槽上向后移动

d) Max slot 是第四个参数,m = 10

e) 最后我想通过将插槽中的数字相加并将其用作0 的小数部分来计算值。

举个例子:

i=2
f(n)=n*2
l=2
m=10

结果应该以这种方式产生:

步骤 1)

02 04 08 16 32 64 128 256 512 1024

第二步)

02 04 08 16 32 64
                1 28
                   2 56
                      5 12
                        10 24

->

slot:     1  2  3  4  5  6  7  8  9  10
computed: 02 04 08 16 32 65 30 61 22 24

步骤 3)

我有一个号码:020408163265306122240.02040816326530612224,如 e) 部分所述。

请注意,如果此示例中的最大插槽更大,则插槽 910 上的数字将发生变化。我还想将 b) 部分作为一个函数,所以我可以将其更改为其他类似 fib(nx) {n1+n2}

我更喜欢 Python 作为算法的计算机语言,但任何易于转换为 Python 的语言都是可以接受的。

添加

这是我迄今为止创建的一个函数:

# l = slot length, doesnt work with number > 2...
def comp(l = 2):

    a = []
    # how to pass a function, that produces this list?
    b = [[0, 2], [0, 4], [0, 8], [1, 6], [3, 2], [6, 4], [1, 2, 8], [2, 5, 6], [5, 1, 2], [1, 0, 2, 4], [2, 0, 4, 8]]

    r = 0
    # main algo
    for bb in b:
        ll = len(bb)
        for i in range(0, ll):
            x = r + i - ll + l
            # is there a better way to do following try except part?
            try:
                a[x] += bb[i]
            except IndexError:
                a.append(bb[i])
            # moving bits backward, any better way to do this?
            s = a[x] - 9
            d = 0
            while s > 0:
                d += 1
                a[x] -= 10
                a[x-d] += 1
                s = a[x-d] - 9
        r += l

    return '0.' + ''.join(map(str, a))

【问题讨论】:

  • 你的问题是什么?
  • 你会为描述的算法创建一个 python 函数并展示给我看吗?
  • 在 SO 上,您几乎没有机会获得对“为我做这一切”请求的积极回复。投反对票的可能性更大。
  • 好的,我马上就准备好一个 sceleton 函数了。

标签: python algorithm decimal


【解决方案1】:
def doub(n):
    return pow(2, n)

def fibo(n):
    a, b = 0, 1
    for i in range(n):
        a, b = b, a + b
    return a

def fixed_slot_numbers(f, l, m):
    b = []
    for n in range(1, m):
        a = [int(c) for c in str(f(n))]
        while len(a) < l:
            a.insert(0, 0)
        b.append(a)
    return b

def algo(function, fixed_slot_length = 2, max_slots = 12):
    a = []
    slot_numbers = fixed_slot_numbers(function, fixed_slot_length, max_slots)
    for r, b in enumerate(slot_numbers):
        r *= fixed_slot_length
        slot_length = len(b)
        for bidx in range(0, slot_length):
            aidx = r + bidx - slot_length + fixed_slot_length
            try:
                a[aidx] += b[bidx]
            except IndexError:
                a.append(b[bidx])
            d = 0
            while a[aidx-d] > 9:
                a[aidx-d] -= 10
                d += 1
                a[aidx-d] += 1
    return '0.%s' % ''.join(map(str, a))


algo(doub, 2, 28) -> 0.020408163265306122448979591836734693877551020405424128 = 1/49

algo(fibo, 1, 28) -> 0.112359550561797752808950848 = 10/89

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2013-06-26
    • 2014-10-17
    • 1970-01-01
    • 2010-12-26
    • 2023-03-06
    相关资源
    最近更新 更多