数学再$ P $大数psentation?大数、数学、psentation

2023-09-11 07:12:04 作者:清酒☘

我试图写一个函数,这需要大量的投入(向上800位长),并没有返回复杂的数学的一个简单的公式作为一个字符串。

I am attempting to write a function which takes a large number as input (upwards of 800 digits long) and returns a simple formula of no complex math as a string.

通过简单的数学,我的意思只是数字是 +, - ,*,/,^ 和()根据需要

By simple math, I mean just numbers with +,-,*,/,^ and () as needed.

'4 ^ 25 + 2 ^ 32 = giveMeMath(1125904201809920); //例如

任何语言会做。我可以重构它,只是寻找一些帮助的逻辑。

Any language would do. I can refactor it, just looking for some help with the logic.

奖金。该输出时间越短越好。处理时间是很重要的。此外,数学精度是必须的。

Bonus. The shorter the output the better. Processing time is important. Also, mathematical accuracy is a must.

更新: 为了澄清,所有的输入值是正整数(没有小数)

Update: to clarify, all input values will be positive integers (no decimals)

推荐答案

这是我在Python的尝试:

Here is my attempt in Python:

def give_me_math(n): 

    if n % 2 == 1:
        n = n - 1  # we need to make every odd number even, and add back one later
        odd = 1
    else:
        odd = 0
    exps = []

    while n > 0:
        c = 0
        num = 0
        while num <= n/2:
            c += 1
            num = 2**c

        exps.append(c)    
        n = n - num
    return (exps, odd)

结果:

>>> give_me_math(100)
([6, 5, 2], 0)  #2**6 + 2**5 + 2**2 + 0 = 100

>>> give_me_math(99)
([6, 5, 1], 1)  #2**6 + 2**5 + 2**1 + 1 = 99

>>> give_me_math(103) 
([6, 5, 2, 1], 1) #2**6 + 2**5 + 2**2 + 2**1 + 1 = 103

我相信结果是准确的,但我不知道你的其他条件。

I believe the results are accurate, but I am not sure about your other criteria.

编辑:

结果:在计算一秒钟

>>> give_me_math(10**100 + 3435)
([332, 329, 326, 323, 320, 319, 317, 315, 314, 312, 309, 306, 304, 303, 300, 298, 295, 294, 289, 288, 286, 285, 284, 283, 282, 279, 278, 277, 275, 273, 272, 267, 265, 264, 261, 258, 257, 256, 255, 250, 247, 246, 242, 239, 238, 235, 234, 233, 227, 225, 224, 223, 222, 221, 220, 217, 216, 215, 211, 209, 207, 206, 203, 202, 201, 198, 191, 187, 186, 185, 181, 176, 172, 171, 169, 166, 165, 164, 163, 162, 159, 157, 155, 153, 151, 149, 148, 145, 142, 137, 136, 131, 127, 125, 123, 117, 115, 114, 113, 111, 107, 106, 105, 104, 100, 11, 10, 8, 6, 5, 3, 1], 1)

800位工作快呢:

800 digit works fast too:

>>> give_me_math(10**800 + 3452)

但输出太长,张贴在这里,这当然是OP的关注。

But the output is too long to post here, which is OPs concern of course.

时间这里复杂性是0(LN(n))的,所以它是pretty的效率

Time complexity here is 0(ln(n)), so it is pretty efficient.