自動スーパー対数を行う


18

正の整数で指定されたN及び数AN番目のテトレーションのように定義される^(^(^(^ ... )))、^はべき乗(または電力)を示し、発現が含ま数を正確にN回。

言い換えれば、テトラションは右結合反復累乗法です。以下のためのN = 4、および= 1.6テトレーションは、1.6 ^(1.6 ^(1.6 ^ 1.6))≈3.5743です。

nに関するテトラションの逆関数は、超対数です。前の例では、4は「スーパーベース」1.6の3.5743のスーパー対数です。

チャレンジ

正の整数nが与えられた場合nがスーパーベースxにおけるそれ自体のスーパー対数になるようなxを見つけます。つまり、見つけるのx、このようなことのx ^(X ^(X ^(... ^ xは)))(と、xが登場するのn倍)に等しいn個

ルール

許可されたプログラムまたは機能。

入出力形式は通常どおり柔軟です。

アルゴリズムは、すべての正の整数に対して理論的に機能するはずです。実際には、メモリ、時間、またはデータ型の制限により、入力は最大値に制限される場合があります。ただし、コードは100少なくとも1分未満で入力に対して機能する必要があります。

アルゴリズムは、理論的には0.001正確な結果を提供する必要があります。実際には、数値計算の累積誤差により、出力精度が低下する場合があります。ただし、0.001指定されたテストケースに対して出力は正確でなければなりません。

最短のコードが優先されます。

テストケース

1    ->  1
3    ->  1.635078
6    ->  1.568644
10   ->  1.508498
25   ->  1.458582
50   ->  1.448504
100  ->  1.445673

リファレンス実装

Matlab / Octaveのリファレンス実装を次に示しますIdeoneで試してください)。

N = 10; % input
t = .0001:.0001:2; % range of possible values: [.0001 .0002 ... 2]
r = t;
for k = 2:N
    r = t.^r; % repeated exponentiation, element-wise
end
[~, ind] = min(abs(r-N)); % index of entry of r that is closest to N
result = t(ind);
disp(result)

N = 10これのために与えるresult = 1.5085

次のコードは、可変精度演算を使用した出力精度のチェックです。

N = 10;
x = 1.5085; % result to be tested for that N. Add or subtract 1e-3 to see that
            % the obtained y is farther from N
s = num2str(x); % string representation
se = s;
for n = 2:N;
    se = [s '^(' se ')']; % build string that evaluates to iterated exponentiation
end
y = vpa(se, 1000) % evaluate with variable-precision arithmetic

これは与える:

  • のためにx = 1.5085y = 10.00173...
  • のためにx = 1.5085 + .001y = 10.9075
  • 以下のためにx = 1.5085 - .001それができますy = 9.23248

そう1.5085で有効なソリューションである.001精度が。


関連。違いは、ここでのスーパー対数の(スーパー)ベースは固定されておらず、結果は一般に整数ではないことです。
ルイスメンドー

関数はかなり早く収束するようです。アルゴリズムが0.001以内の曲線近似である場合、すべての正の整数に対して理論的に機能していると見なされますか?
-xnor


@KevinCruijssenかなり高速なバイナリ検索に基づいたMatlabのリファレンス実装があります。役立つ場合は投稿できます
ルイスメンドー

1
DOESはxとして収束n無限大に近づきますか?
mbomb007

回答:



10

Haskell、55 54 52バイト

s n=[x|x<-[2,1.9999..],n>iterate(x**)1!!floor n]!!0

使用法:

> s 100
1.445600000000061

1バイトの@nimiに感謝します!
@xnor for 2に感謝します!


1
[ ]!!0head[ ]バイトを保存する代わりに
-nimi

1
s n=[x|x<-[2,1.9999..],n>iterate(x**)1!!n]!!0Haskellにそのタイプを受け入れさせることができれば、より短くなります。
xnor

@xnor実際にそれを書いたとき、私はイテレートをいじりましたが、どういうわけか、それは当時より長くなった
-BlackCap

6

Javascript、ES6:77バイト/ ES7:57 53バイト

ES6

n=>eval("for(x=n,s='x';--x;s=`Math.pow(x,${s})`);for(x=2;eval(s)>n;)x-=.001")

ES7

使用**DanTheManによって示唆されているように:

n=>eval("for(x=2;eval('x**'.repeat(n)+1)>n;)x-=.001")

let f =
n=>eval("for(x=n,s='x';--x;s=`Math.pow(x,${s})`);for(x=2;eval(s)>n;)x-=.001")

console.log(f(25));


ES7を使用する場合、の**代わりに使用できますMath.pow
DanTheMan

4

Mathematica、40 33バイト

マーフィーのおかげで、ほぼ20%の節約になりました!

1//.x_:>x+.001/;Nest[x^#&,1,#]<#&

Nest[x^#&,1,n]xのn番目の四分割を生成します。したがってNest[x^#&,1,#]<#、xの(入力)番目の四分円が(入力)より小さいかどうかをテストします。単純にx = 1から開始し、テトラションが大きすぎるまで0.001を繰り返し追加し、最後のx値を出力します(したがって、答えは正確な値よりも大きいが、0.001以内であることが保証されます)。

私はゆっくりと学んでいるので、//.x_:>y/;zまたは//.x_/;z:>y「テンプレートxに一致するものを探しますが、テストzがtrueを返すもののみを探し、ルールyによってxを操作し、何も変わらないまで繰り返します」という意味です。ここでは、テンプレートx_は「私が見る任意の数」ですが、他のコンテキストではさらに制約される可能性があります。

入力が少なくとも45の場合、テトラクションは非常に急速に増加するため、最後のステップでオーバーフローエラーが発生します。ただし、xの値は引き続き更新され、正しく出力されます。ステップサイズを0.001から0.0001に減らすと、最大112の入力に対するこの問題が修正され、ブートに対するより正確な答えが得られます(そして、約1/4秒でまだ迅速に実行されます)。しかし、それは余分な1バイトなので、忘れてください!

元のバージョン:

x=1;(While[Nest[x^#&,1,#]<#,x+=.001];x)&

それを少しゴルフ:1//.x_:>x+.001/;Nest[x^#&,1,#]<#&
マーフィー

@murphy:すばらしい!私はまだ//.助けなしで使用できるポイントに到達することを誓います:)
グレッグマーティン

4

J、39 31 28バイト

(>./@(]*[>^/@#"0)1+i.%])&1e4

リファレンス実装に基づきます。小数点以下3桁まで正確です。

@Adámのソリューションのメソッドを使用して8バイトを保存しました。

使用法

複数の入出力をフォーマットするために使用される追加コマンド。

   f =: (>./@(]*[>^/@#"0)1+i.%])&1e4
   (,.f"0) 1 3 6 10 25 50 100
  1      0
  3  1.635
  6 1.5686
 10 1.5084
 25 1.4585
 50 1.4485
100 1.4456
   f 1000
1.4446

説明

(>./@(]*[>^/@#"0)1+i.%])&1e4  Input: n
                         1e4  The constant 10000
(                      )      Operate on n (LHS) and 10000 (RHS)
                   i.           The range [0, 10000)
                      ]         Get (RHS) 10000
                     %          Divide each in the range by 10000
                 1+             Add 1 to each
     (          )               Operate on n (LHS) and the range (RHS)
             #"0                  For each in the range, create a list of n copies
          ^/@                     Reduce each list of copies using exponentation
                                  J parses from right-to-left which makes this
                                  equivalent to the tetration
        [                         Get n
         >                        Test if each value is less than n
      ]                           Get the initial range
       *                          Multiply elementwise
 >./@                           Reduce using max and return

4

Python、184バイト

def s(n):
 def j(b,i):
  if i<0.1**12:
   return b
  m = b+i
  try:
   v = reduce(lambda a,b:b**a,[m]*n)
  except:
   v = n+1
  return j(m,i/2) if v<n else j(b,i/2)
 return j(1.0,0.5)

テスト出力(実際の印刷ステートメントをスキップ):

   s(1) 1.0
   s(3) 1.63507847464
   s(6) 1.5686440646
  s(10) 1.50849792026
  s(25) 1.45858186605
  s(50) 1.44850389566
 s(100) 1.44567285047


s(1000000)非常に高速に計算されます
mbomb007

3

ラケット187バイト

(define(f x n)(define o 1)(for((i n))(set! o(expt x o)))o)
(define(ur y(l 0.1)(u 10))(define t(/(+ l u)2))(define o(f t y))
(cond[(<(abs(- o y)) 0.1)t][(> o y)(ur y l t)][else(ur y t u)]))

テスト:

(ur 1)
(ur 3)
(ur 6)
(ur 10)
(ur 25)
(ur 50)
(ur 100)

出力:

1.028125
1.6275390625
1.5695312499999998
1.5085021972656247
1.4585809230804445
1.4485038772225378
1.4456728475168346

詳細バージョン:

(define (f x n)
  (define out 1)
  (for((i n))
    (set! out(expt x out)))
  out)

(define (uniroot y (lower 0.1) (upper 10))
  (define trying (/ (+ lower upper) 2))
  (define out (f trying y))
  (cond
    [(<(abs(- out y)) 0.1)
     trying]
    [(> out y)
     (uniroot y lower trying)]
    [else
      (uniroot y trying upper)]))

2

Perl 6、42バイト

{(0,.00012).min:{abs $_-[**] $^r xx$_}}

(サンプルMatlabコードの翻訳)

テスト:

#! /usr/bin/env perl6
use v6.c;
use Test;

my &code = {(0,.00012).min:{abs $_-[**] $^r xx$_}}

my @tests = (
  1   => 1,
  3   => 1.635078,
  6   => 1.568644,
  10  => 1.508498,
  25  => 1.458582,
  50  => 1.448504,
  100 => 1.445673,
);

plan +@tests + 1;

my $start-time = now;

for @tests -> $_ ( :key($input), :value($expected) ) {
  my $result = code $input;
  is-approx $result, $expected, "$result ≅ $expected", :abs-tol(0.001)
}

my $finish-time = now;
my $total-time = $finish-time - $start-time;
cmp-ok $total-time, &[<], 60, "$total-time.fmt('%.3f') is less than a minute";
1..8
ok 1 - 1  1
ok 2 - 1.6351  1.635078
ok 3 - 1.5686  1.568644
ok 4 - 1.5085  1.508498
ok 5 - 1.4586  1.458582
ok 6 - 1.4485  1.448504
ok 7 - 1.4456  1.445673
ok 8 - 53.302 seconds is less than a minute


1

公理587バイト

l(a,b)==(local i;i:=1;r:=a;repeat(if i>=b then break;r:=a^r;i:=i+1);r);g(q,n)==(local r,y,y1,y2,t,v,e,d,i;n<=0 or n>1000 or q>1000 or q<0 => 0;e:=1/(10**(digits()-3));v:=0.01; d:=0.01;repeat(if l(v,n)>=q then break;v:=v+d;if v>=1 and n>25 then d:=0.001;if v>=1.4 and n>40 then d:=0.0001;if v>=1.44 and n>80 then d:=0.00001;if v>=1.445 and n>85 then d:=0.000001);if l(v-d,n)>q then y1:=0.0 else y1:=v-d;y2:=v;y:=l(v,n);i:=1;if abs(y-q)>e then repeat(t:=(y2-y1)/2.0;v:=y1+t;y:=l(v,n);i:=i+1;if i>100 then break;if t<=e then break;if y<q then y1:=v else y2:=v);if i>100 then output "#i#";v)

少ないゴルフ+数字

l(a,b)==
  local i
  i:=1;r:=a;repeat(if i>=b then break;r:=a^r;i:=i+1)
  r
g(q,n)==
 local r, y, y1,y2,t,v,e,d, i
 n<=0 or n>1000 or q>1000 or q<0 => 0  
 e:=1/(10**(digits()-3))
 v:=0.01; d:=0.01  
 repeat  --cerco dove vi e' il punto di cambiamento di segno di l(v,n)-q
    if l(v,n)>=q then break
    v:=v+d 
    if v>=1     and n>25 then d:=0.001
    if v>=1.4   and n>40 then d:=0.0001
    if v>=1.44  and n>80 then d:=0.00001
    if v>=1.445 and n>85 then d:=0.000001
 if l(v-d,n)>q then y1:=0.0
 else               y1:=v-d 
 y2:=v; y:=l(v,n); i:=1  -- applico il metodo della ricerca binaria
 if abs(y-q)>e then      -- con la variabile i di sicurezza
    repeat 
       t:=(y2-y1)/2.0; v:=y1+t; y:=l(v,n)
       i:=i+1
       if i>100 then break
       if t<=e  then break 
       if  y<q  then y1:=v
       else          y2:=v
 if i>100 then output "#i#"
 v

(3) -> [g(1,1), g(3,3), g(6,6), g(10,10), g(25,25), g(50,50), g(100,100)]
   Compiling function l with type (Float,PositiveInteger) -> Float
   Compiling function g with type (PositiveInteger,PositiveInteger) ->
      Float

   (3)
   [1.0000000000 000000001, 1.6350784746 363752387, 1.5686440646 047324687,
    1.5084979202 595960768, 1.4585818660 492876919, 1.4485038956 661040907,
    1.4456728504 738144738]
                                                             Type: List Float

1

Common Lisp、207バイト

(defun superlog(n)(let((a 1d0)(i 0.5))(loop until(< i 1d-12)do(let((v(or(ignore-errors(reduce #'expt(loop for q below n collect(+ a i)):from-end t))(1+ n))))(when(< v n)(setq a (+ a i)))(setq i(/ i 2)))) a))

reducewith :from-end tを使用すると、「逆累乗」中間ラムダを実行する必要がなくなり(lambda (x y) (expt y x))ます(基本的に、14バイト(リムーバブルスペースを削除した場合は12バイト)を節約できます)。

フロートオーバーフローを処理する必要がありignore-errorsますnilが、エラーが発生するとフォームが返さorれるため、デフォルト値を提供するために使用できます。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.