幾何学的シーケンスを推測する


18

Haskellには、この3つの数字を与えることができ、それらから算術シーケンスを推測できるこのすっきりした(-見える)機能があります。たとえば、[1, 3..27]はと同等[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27]です。

それはクールで、算術シーケンス以外はすべてかなり制限されています。また、pfft。乗算はどこにあるのか [1, 3..27]戻るような幾何学的なシーケンスを実行した方がクールではないでしょう[1, 3, 9, 27]か?

チャレンジ

書き込みプログラム/機能 3つの正の整数かかり、B、及びCと出力xは最大整数≤であるCとして表すことができるここでnは正の整数です。[a, b, b × (b ÷ a), b × (b ÷ a)2, ..., x]b × (b ÷ a)n

つまり、出力はrである必要があります。

r0 = a
r1 = b
rn = b × (b ÷ a)n-1
rlast = greatest integer ≤ c that can be represented as b × (b ÷ a)n
         where n is a positive integer

仕様書

  • 標準のI / Oルールが 適用されます
  • 標準的な抜け穴禁止されています。
  • Bは常に割り切れるとなります。
  • < BC
  • この課題は、すべての言語で最短のアプローチを見つけることではなく、各言語で最短のアプローチを見つけることです
  • あなたのコードがされるバイト数で得点特に指定がない限り、通常はエンコーディングUTF-8で、。
  • このシーケンスを計算する組み込み関数(Mathematicaは1つ:Pを持っているかもしれません)は許可されていますが、組み込みに依存しないソリューションを含めることをお勧めします。
  • 「実用的な」言語であっても説明が奨励されます。

テストケース

a   b   c     r

1   2   11    [1, 2, 4, 8]
2   6   100   [2, 6, 18, 54]
3   12  57    [3, 12, 48]
4   20  253   [4, 20, 100]
5   25  625   [5, 25, 125, 625]
6   42  42    [6, 42]

いくつかのより良い形式で:

1 2 11
2 6 100
3 12 57
4 20 253
5 25 625
6 42 42

1, 2, 11
2, 6, 100
3, 12, 57
4, 20, 253
5, 25, 625
6, 42, 42

@Adám番号(最初のテストケースを参照)
-user202729

1
式は単純にb ^ n / a ^ n-1であることに注意してください。n = 0
H.PWiz

2
もちろんMathematicaにはビルトインがあります
ニール

結果が浮動小数点エラーのために正確に整数でない場合、それは受け入れられますか?
ルイスメンドー

@LuisMendoはい。
完全に人間の

回答:


6

、8バイト

~↑≤Ṡ¡o//

入力はb、c、aの順です。 オンラインでお試しください!

説明

~↑≤Ṡ¡o//  Implicit inputs.
       /  a/b as exact rational number.
     o/   Divide by a/b (so multiply by b/a).
    ¡     Iterate that function
   Ṡ      on a. Result is the infinite list [a, b, b^2/a, b^3/a^2, ..
 ↑        Take elements from it while
~ ≤       they are at most c.

このプログラムの制御フローは、従うのが少し難しいです。まず、Bは右端に供給される/関数を生成/bすることによって除算そのB。次に、~残りのプログラムを3つの部分に分割します~(↑)(≤)(Ṡ¡o//b)。これは飼料Cとへ、として結果を結合します。の結果は、引数が最大cであるかどうかをチェックし、これが保持する要素の最長のプレフィックスをとる関数です。Ṡ¡o//b≤c↑≤c

(Ṡ¡o//b)a目的の無限リストにどのように評価されるかを示すことは残っています。括弧内の部分はに分割されṠ(¡)(o//b)ます。その後フィードに、に結果を供給し、その後、与え2番目の引数に。式はで数および除算それを取る関数与え/ bを、とある2番目の引数、上でこの機能を反復します。o//b¡(o//b)a¡

説明を視覚化する一連の変換を次に示します。

  (~↑≤Ṡ¡o//) b c a
= (~↑≤Ṡ¡o/(/b)) c a
= ~(↑)(≤)(Ṡ¡o/(/b)) c a
= ↑(≤c)((Ṡ¡o/(/b)) a)
= ↑(≤c)(Ṡ(¡)(o/(/b)) a)
= ↑(≤c)(¡(o/(/b)a) a)
= ↑(≤c)(¡(/(/ba))a)
Last line in English: takeWhile (atMost c) (iterate (divideBy (divideBy b a)) a)

明示的な変数をa、b、cの順序で使用する代替ソリューション:

↑≤⁰¡*/⁵²



3

JavaScript(ES6)、41 37バイト

@Neilのおかげで4バイト節約

入力をとして受け取ります(b,c)(a)

(b,c)=>g=a=>a>c?[]:[a,...g(b,b*=b/a)]

テストケース

コメント済み

(b, c) =>                 // main function taking b and c
  g = a =>                // g = recursive function taking a
    a > c ?               //   if a is greater than c:
      []                  //     stop recursion and return an empty array
    :                     //   else:
      [ a,                //     return an array consisting of a, followed by 
        ...g(             //     the expanded result of a recursive call to g()
          b,              //       with a = b
          b *= b / a      //       and b = b * ratio
        ) ]               //     end of recursive call

1
議論を整理することで私は得られます(b,c)=>g=a=>a>c?[]:[a,...g(b,b*=b/a)]
ニール



2

Python 3、93 90 74 73バイト

x=lambda a,b,c,i=0,q=[]:a*(b/a)**i>c and q or x(a,b,c,i+1,q+[a*(b/a)**i])

オンラインで試す

Roduser202729のおかげで、かなりのバイト数を減らすことができました!


1
def + return -> lambda。Pythonのヒント。
user202729

1
また、できimport*ます。
user202729

1
あなたが使用することができますwhile i<=c:i++バイトの多くを保存するために(代わりにリスト内包+ログ)
ロッド

@Rodログなしでwhileループを使用するにはどうすればよいですか?idkの反復時間
マニッシュクン




1

05AB1E、12バイト

順番に入力 c,b,a

ÝmI¹Ý<m/ʒ¹›_

オンラインでお試しください!

説明

Ý              # push the range [0 ... c]
 m             # raise b to the power of each
  I            # push a
   ¹Ý          # push the range [0 ... c]
     <         # decrement each
      m        # push a to the power of each
       /       # elementwise division of ranges
        ʒ      # filter, keep only elements that are
         ¹›_   # not greater than c

1

MATL、17バイト

t:,qtiw^w]x/tb>~)

オンラインでお試しください!

MATLでボールを転がすだけです。これを解決するより冗長な方法がないとは想像できません。


1
...トリプル否定はしないでください。
user202729

2
@ user202729偶然ではなかったことに気付かなかった。:)
18

「それが意図せずに行われなかったことをどのようにして得られなかったかわからない」という意味ではありませんか:P
HyperNeutrino

@HyperNeutrino番号
Sanchises



1

MATL、12バイト

y/ivZlZ}3$:W

オンラインでお試しください!または、すべてのテストケースを確認します

説明

y     % Implicitly take two inputs, and duplicate the first onto the top
/     % Divide
i     % Take third input
v     % Vertically concatenate the three numbers into a column vector
Zl    % Binary logarithm, element-wise
Z}    % Split the vector into its three components
3$:   % Three-input range. Arguments are start, step, upper limit
W     % 2 raised to that, element-wise. Implicit display

1
これは本当にいいです。私は再利用で苦しんでいたac(私が始まる多くの失敗した試みを持っているy/i)が、この方法を使用して、あなたはきちんと一緒にすべてを保持します。
Sanchises

1
このアプローチは、実際にはOctaveでも3バイト短くなりました。
-Sanchises

0

Perl、38バイト

含める+3ために-nuse 5.10.05.10機能のロックを解除して自由です)

#!/usr/bin/perl -n
use 5.10.0;
/ \d+/;say,$_*=$&/$`until($_+=0)>$'

次に、次のように実行します:

geosequence.pl <<< "1 3 26"


0

Japt、14バイト

ÆWpX zVpXÉÃf§U

それを試してみてください


説明

                    :Implicit input of integers U=c, V=a & W=b
Æ         Ã         :Range [0,U) and pass each X through a function
 WpX                :  W to the power of X
     z              :  Floor divide by
      VpXÉ          :  V to the power of X-1
           f§U      :Filter elements less than or equal to U


0

TI-BASIC、31バイト

ユーザーからの入力を受け取り、で出力しAnsます。私はc = b n / a n-1でnを解き、n = 1 + ln(c / b)/ ln(b / a)を得ました。これは、n = 1 + log b / a(c / b)と同じです。ゴルフの目的のために、シーケンスを-1で開始し、0〜nではなくn-1で終了します。

Prompt A,B,C
seq(B(B/A)^N,N,-1,logBASE(C/B,B/A

0

APL(Dyalog Unicode)、38バイト

{(g≤⊃⌽⍵)⊆gf,(⍵[1]*p+1)÷(f←⊃⍵)*p←⍳⊃⌽⍵}

オンラインでお試しください!

プレフィックスDfn。入力を順番受け取りa b c、使用します⎕IO←0I ndex O rigin)

@ErikTheOutgolferのおかげで、投稿する前にこれを6バイト削り取ってくれました。

どうやって?

{(g≤⊃⌽⍵)⊆gf,(⍵[1]*p+1)÷(f←⊃⍵)*p←⍳⊃⌽⍵}  Prefix Dfn. Input  is a vector
                                    ⌽⍵   Reverse ⍵. Yields c b a
                                        Pick the first element (c)
                                        Index. Yields the integers 0..c-1
                                p       Assign to the variable p
                               *         Exponentiate
                         (f←⊃⍵)          Pick the first element of  (a) and assign to f
                                         This yields the vector (a^0, a^1, ..., a^c-1)
                        ÷                Element-wise division
                    p+1)                 The vector 1..c
                   *                     Exponentiate
              (⍵[1]                      Second element (because of IO0) of  (b)
                                         This yields the vector (b^1, b^2, ..., b^c)
            f,                           Prepend f (a). This yields the vector 
                                         (a, b^1/a^0, b^2/a^1, ...)
          g                             Assign the vector to g
                                        Partition. This takes a boolean vector as left
                                         argument and drops falsy elements of the right argument.
     ⊃⌽⍵)                                Pick the last element of  (c)
  (g                                    Check if each element of gc. Yields the boolean
                                         vector that is the left argument for 

0

スタックス、14 バイトCP437

ü╞¥ß¥║/,5å╘⌂åº

解凍すると16バイト、

E~Y/y{;^<}{[*gfm

オンラインで実行してデバッグします!

の形式で入力を受け取ります[b, a, c]

@recursiveがより良い解決策を持っていることは間違いありません。

説明

E~                              Parse  input, put `c` on input stack
  Y/                            Store `a` in register `y` and calculate `b`/`a`
    y                           Put `y` back to main stack, stack now (from top to bottom): [`a`, `b`/`a`]
     {   }{  gf                 generator
      ;^<                       Condition: if the generated number is smaller than the top of input stack (i.e. `c`)
           [*                   duplicate the second item in main stack and multiply it with the item at the top
                                   i.e. multiply last generated value by `b/a` and generate the value
              m                 Output array, one element on each line


0

C(gcc)、82バイト

n;f(a,b,c){float r=0;for(n=0;r<=c;)(r=pow(b,n)/pow(a,n++-1))<=c&&printf("%f ",r);}

オンラインでお試しください!

r_n = b^n/a^(n-1)まで計算して印刷しr_n > cます。

でコンパイルする必要があります-lm


69バイトn;f(a,b,c){for(float r=n=0;r=pow(b/a,n++)*a,r<=c&&printf("%f ",r););}
-ceilingcat

0

APL(Dyalog)、23バイト(SBCS

これは、引数abを左側に、cを右側に取ります。

{⊃(⍵∘≥⊆⊢)⊣/⍵2⍴⍺,÷\⍵⍴⌽⍺}

オンラインでお試しください!

おそらくより短い方法がありますが、私は ÷\がかわいい。

説明:

{...}匿名関数⍺はa bですc。まあ言ってみればa b c = 2 6 100

⌽⍺リバース6 2

⍵⍴繰り返し回数:6 2 6 2 6 2 6 2 ...

÷\ プレフィックスの除算: 6 (6÷2) (6÷(2÷6)) (6÷(2÷(6÷2))).. = 6 3 18 9 54 ..

⍺,付加2 6 6 3 18 9 54 27 162 81 ...

⊣/⍵2⍴ 他のすべての要素(および末尾の繰り返し)を取得します。

  ⍵2⍴作る2からカラムマトリックスを2 6 6 3 18 9 54 ...

  ⊣/ 最初の列を取得する

⊆⊢ 配列をブロックに分割します

⍵∘≥ すべての要素以上

最初のそのようなブロックを取る

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