计算数字的超级根

10

• 如果A = `65,536`和B = `4`则打印`2`
• 如果A = `7,625,597,484,987`和B = `3`则打印`3`

A和B是正整数，结果必须是小数点后5位数精度的浮点数。结果属于真实域。

1

2014年

3

Johannes H.

5

Mhmd 2014年

2

Tobia 2014年

1

6

C-为了清晰起见，没有试图压缩代码

``````A: A ∈ ℝ, A ≥ 1.0
B: B ∈ ℕ, B ≥ 1
``````

``````#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#define TOLERANCE    1.0e-09

double tetrate(double, int);

int main(int argc, char **argv)
{
double target, max, min, mid, working;
int levels;

if (argc == 3)
{
target = atof(argv[1]); // A
levels = atoi(argv[2]); // B

// Shortcut if B == 1
if (levels == 1)
{
printf("%f\n", target);
return 0;
}

// Get a first approximation
max = 2.0;
while (tetrate(max, levels) < target)
max *= 2.0;

min = max / 2.0;

// printf("Answer is between %g and %g\n", min, max);

// Use bisection to get a closer approximation
do
{
mid = (min + max) / 2.0;
working = tetrate(mid, levels);
if (working > target)
max = mid;
else if (working < target)
min = mid;
else
break;
}
while (max - min > TOLERANCE);

// printf("%g: %f = %f tetrate %d\n", target, tetrate(mid, levels), mid, levels);
printf("%f\n", mid);
}

return 0;
}

double tetrate(double d, int i)
{
double result = d;

// If the result is already infinite, don't tetrate any more
while (--i && isfinite(result))
result = pow(d, result);

return result;
}
``````

``````gcc -o tet_root tet_root.c -lm
``````

``````./tet_root A B
``````

4 2

``````\$ ./tet_root 65536 4
2.000000
``````

3 3

``````\$ ./tet_root 7625597484987 3
3.000000
``````

3个 π

``````\$ ./tet_root 1.340164183e18 3
3.141593
``````

n）➙2为n∞？（众所周知的限制）

``````\$ ./tet_root 2 10
1.416190

\$ ./tet_root 2 100
1.414214

\$ ./tet_root 2 1000
1.414214
``````

n（e 1 / e）∞为n∞？（上限）

``````\$ ./tet_root 9.999999999e199 100
1.445700

\$ ./tet_root 9.999999999e199 1000
1.444678

\$ ./tet_root 9.999999999e199 10000
1.444668

\$ ./tet_root 9.999999999e199 100000
1.444668
``````

Albert Renshaw

@AlbertRenshaw这只是二等分的实现。一点也不难。
Simply Beautiful Art

5

Python，87个字符

``````E=lambda x,n:x**E(x,n-1)if n else 1
def S(A,B):
x=1.
while E(x,B)<A:x+=1e-5
return x
``````

``````>>> 1e200*1e200
inf
>>> 1e200**2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: (34, 'Numerical result out of range')
``````

2014年

danmcardle 2014年

2
@Josh：我报告了一个错误：bugs.python.org/issue20543 基本上，按预期方式工作-对于IEEE浮动，它们并不多。如果他们要解决任何问题，则`OverflowError`在第一种情况下将生成一个。

3

Mathematica，35 40

``````n /. Solve[Nest[#^(1/n) &, a, b] == n]~N~5
``````

``````n /. Last@Solve[Nest[#^(1/n) &, a, b] == n]~N~5
``````

2

朱莉亚

``````julia> t(a,b)=(c=a;for j=1:b-1;c=a^c;end;c)
julia> s(c,b)=(i=1;while t(i,b)!=c;i+=1;end;i)
julia> s(65536,4)
2
julia> s(7625597484987,3)
3
``````

2

APL，33个字符

``````{r←⍵⋄⍺{1≥⍵⍟⍣⍺⊢r:⍵⋄⍺∇⍵+i}1+i←1e¯6}
``````

日志Ç日志Ç登录Ç ⋯A≤1
，其中该日志Ç函数应用递归乙倍。

``````      4 {r←⍵⋄⍺{1≥⍵⍟⍣⍺⊢r:⍵⋄⍺∇⍵+i}1+i←1e¯6} 65536
2.0000009999177335
3 {r←⍵⋄⍺{1≥⍵⍟⍣⍺⊢r:⍵⋄⍺∇⍵+i}1+i←1e¯6} 7625597484987
3.0000000000575113
``````

APL，对数复杂度

时间= O（B×对数（C）+ B×对数（D））

``````sroot←{              ⍝ Compute the ⍺-th order super-root of ⍵:
n←⍺ ⋄ r←⍵          ⍝ n is the order, r is the result of the tetration.
u←{                ⍝ Compute u, the upper bound, a base ≥ the expected result:
1≥⍵⍟⍣n⊢r:⍵       ⍝   apply ⍵⍟ (log base ⍵) n times; if ≤1 then upper bound found
∇2×⍵             ⍝   otherwise double the base and recurse
}2                 ⍝ start the search with ⍵=2 as a first guess.
(u÷2){             ⍝ Perform a binary search (bisection) to refine the base:
b←(⍺+⍵)÷2        ⍝   b is the middle point between ⍺ and ⍵
t←b⍟⍣n⊢r         ⍝   t is the result of applying b⍟ n times, starting with r;
t=1:b            ⍝   if t=1 (under ⎕CT), then b is the super-root wanted;
t<1:⍺∇b          ⍝   if t<1, recurse between ⍺ and b
b∇⍵              ⍝   otherwise (t>1) returse between b and ⍵
}u                 ⍝ begin the search between u as found earlier and its half.
}
``````

``````      4 sroot 65536
1.9999999999999964
4 sroot 65537
2.000000185530773
3 sroot 7625597484987
3
3 sroot 7625597400000
2.999999999843567
3 sroot 7625597500000
3.000000000027626
``````

“ 3”作为精确值出现是因为它恰好是二进制搜索直接命中的值之一（从2开始，加倍到4，对分到3）。在一般情况下不会发生，因此结果将以⎕CT误差近似于根值（更准确地说，每个候选碱基的对数测试均以⎕CT公差执行。）

1

Ruby，79个字节

``````->a,b{x=y=1.0;z=a;eval"y=(x+z)/2;x,z=a<eval('y**'*~-b+?y)?[x,y]:[y,z];"*99;p y}
``````

Ruby，87个字节

``````->a,b{x=y=1.0;z=a;(y=(x+z)/2;x,z=a<eval("y**"*~-b+?y)?[x,y]:[y,z])while y!=(x+z)/2;p y}
``````

``````-> a, b {
# y^^b by evaluating the string "y ** y ** ..."
tetration =-> y {eval "y ** " * (b-1) + ?y}

lower = middle = 1.0
upper = a

while middle != (lower + upper) / 2 do
middle = (lower + upper) / 2

if tetration[middle] > a
upper = middle
else
lower = middle
end
end

print middle
}
``````

0

k [52个字符]

``````{{{((%x)*(z*x-1)+y%z xexp x-1)}[x;y]/[2]}[y]/[y<;x]}
``````

``````{{{((%x)*(z*x-1)+y%z xexp x-1)}[x;y]/[2]}[y]/[y<;x]}[7625597484987;3]
3f

{{{((%x)*(z*x-1)+y%z xexp x-1)}[x;y]/[2]}[y]/[y<;x]}[65536;4]
2f
``````

0

哈斯克尔

``````{-
The value of a is the result of exponentiating b some number of times.
This function computes that number.
-}
superRoot a b = head [x | x<-[2..a], tetrate x b == a]

{-
compute b^b^...^b repeated n times
-}
tetrate b 1 = b
tetrate b n = b^(tetrate b (n-1))
``````

``````*Main> superRoot 65536 4
2
*Main> superRoot 7625597484987 3
3
``````

0

Mathematica，41个字节，无优化

Mathematica基本上是为解决此类问题而发明的。一种简单的解决方案是将问题构造为嵌套幂级数，然后将其传递给内置`Reduce`函数，该函数寻求方程的解析解。结果，除了异常简洁的代码外，以下内容也不是蛮力的。

``````Reduce[Nest[Power[#, 1/x] &, a, b] == x, x, Reals]
``````

0

05AB1E，16 个字节

``````1[ÐU²FXm}¹@#5(°+
``````

``````1                 # Push a 1
[                # Start an infinite loop:
Ð               #  Triplicate the top value on the stack
U              #  Pop and store one in variable `X`
²F            #  Inner loop the second input amount of times:
Xm          #   And take the top value to the power `X`
}         #  After the inner loop:
¹@       #  If the resulting value is larger than or equal to the first input:
#      #   Stop the infinite loop
#   (after which the top of the stack is output implicitly as result)
5(°+  #  If not: increase the top value by 10^-5``````

`ÐU²FXm}`也可能`D²>и.»m`是相同的字节数：

``````  D               #   Duplicate the top value on the stack
²>             #   Push the second input + 1
и            #   Repeat the top value that many times as list
.»          #   Reduce it by:
m         #    Taking the power``````