相互模倣


17

してみましょうAからなる正の整数小数点以下の桁。してみましょう別の正の整数です。nd1,d2,...,dnB

このチャレンジのために、次のような正の整数リストが少なくとも1つ存在する場合、をコピーキャットと呼びます。AB P 1P 2p nBp1,p2,...,pn

i=1ndipi=B

AB A B B Aと呼ばれている相互の模倣場合の模倣であるとの模倣である。BABBA

526853およびは、相互模倣です。853

53+29+63=853

そして:

83+51+32=526

チャレンジ

2つの正の整数と与えられた場合、とが相互コピーキャットであ​​る場合は真実の値を出力するか、そうでない場合は偽の値を出力することがタスクです。ABAB

明確化と規則

  • あなたはとることができといずれかの合理的な、明確な形式で(例えば整数、文字列、数字のリスト、...)AB
  • ABとは等しい場合があります。数値がそれ自体の相互的な模倣である場合、それはA007532に属します。B
  • 真偽値の代わりに、2つの異なる一貫した値を返す場合があります。
  • 以下のためにと、あなたのコードはに完了しなければならない1分未満。ただし、値を大きくするのに時間がかかりすぎる場合は、理論的にそれらを解決できる必要があります。1A<10001B<1000
  • これはです。

テストケース

Truthy:
1 1
12 33
22 64
8 512
23 737
89 89
222 592
526 853
946 961
7 2401
24 4224
3263 9734
86 79424
68995 59227
32028 695345

Falsy:
1 2
3 27
9 24
24 42
33 715
33 732
222 542
935 994
17 2401
8245 4153

推奨されるケース:17 2401 -> false。私はこれにほとんどつまずいています。
朝琴シエル

回答:


8

Brachylog、19バイト

ẹ{∧ℕ₁;?↔^}ᵐ².+ᵐ↔?∧≜

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

出力true.またはfalse.

説明

ẹ                     Split the numbers into lists of digits
 {       }ᵐ²          For each digit
  ∧ℕ₁                 Let I be a strictly positive integer
     ;?↔^                Compute the digit to the power I (which is unknown currently)
            .         Call . the list of those new numbers
            .+ᵐ       Their mapped sum results…
               ↔?     …in the reverse of the input
                 ∧≜   Find if there effectively are values for the numbers in . to satisfy
                        these relationships

2
@Arnauld 1バイトのコストで修正されました。私がチェックした方法で動作しない2401a 0が含まれていたため、失敗しました(これは、バイトを節約するためIに両方Iと数字にマッピングしたため)
Fatalize

6

、17バイト

Λλ€⁰mΣΠTṪ^ḣ√⁰d)De

オンラインでお試しください! 1000未満のすべてのテストケースを約11秒で終了します。

説明

Λλ€⁰mΣΠTṪ^ḣ√⁰d)De  Implicit inputs, say 12 and 33.
                e  Put into a list: [12,33]
               D   Duplicate: [12,33,12,33]
Λ                  Does this hold for all adjacent pairs:
                    (12,33 is checked twice but it doesn't matter)
                    For example, arguments are 33 and 12.
 λ            )     Anonymous function with arguments 33 (explicit) and 12 (implicit).
             d      Base-10 digits of implicit argument: [1,2]
          ḣ√⁰       Range to square root of explicit argument: [1,2,3,4]
        Ṫ^          Outer product with power: [[1,2],[1,4],[1,8],[1,16],[1,32]]
       T            Transpose: [[1,1,1,1,1],[2,4,8,16,32]]
      Π             Cartesian product: [[1,2],[1,4],...,[1,32]]
    mΣ              Map sum: [3,5,...,33]
  €⁰                Is the explicit argument in this list? Yes.

なぜ機能するのか

B=d1p1++dnpndipidipiBipilogdiBdi1011piB1piBlogdiBBdi3Bdi=2log2B>BB=823=812A223A=210kkA8B とにかく、プログラムは他の計算に関係なく、偽の値を正しく返します。


ハスクを学びたいと思う素晴らしい答え。2つの質問:1.導入後、暗黙の引数について再度言及します。いつ使用されますか?2.このアルゴリズムがOPで提示されたアルゴリズムと同等である理由について詳しく説明してください。
ジョナ

1
@Jonah 1. digit関数dは暗黙の引数を取ります。説明でこれを明確にした。2.プログラムの正確さについての議論を追加しました。
ズガーブ

ありがとう...ところで、私を混乱させた部分は「すべての人のリストはどこから来たのですか?」でした....私は今、これが単に1の力がすべて1であるためだと気づきました...
ジョナ


4

05AB1E26 22 バイト

εVтLIàgãεYSym}OIyKå}˜P

入力をリスト(例:)として受け取ります[526,853]

オンラインで試す、範囲内のほとんどのテストケースを確認し[1,999]てください

[1,n]リストがにハードコーディングされ[1,100]、入力マッピングごとに1回、デカルトリストを2回作成することを除いて、以下の古い回答と同様です。これはパフォーマンスの主なボトルネックです。


古い26バイトの回答は、パフォーマンスに優れています。

Z©bgL®gãUεVXεYSym}OsN>èå}P

このバージョンでは、パフォーマンスを大幅に向上[1,1000]させて簡単に実行できるように、いくつかのバイトを交換しました。範囲内の数値を含むテストケースは、[1,9999]TIOで約1秒で実行されます。[10000,99999]TIOで約10〜15秒の範囲のテストケース。それを超えるとタイムアウトします。

オンラインで試す、範囲内の数値ですべてのテストケースを検証し[1,9999]てください

説明:

Z                 # Push the max of the (implicit) input-list (without popping)
                  #  i.e. [526,853] → 853
 ©                # Store it in the register (without popping)
  b               # Convert to binary
                  #  i.e. 853 → 1101010101
   g              # Take its length
                  #  i.e. 1101010101 → 10
    L             # Pop and push a list [1, n]
                  #  i.e. 10 → [1,2,3,4,5,6,7,8,9,10]
     ®            # Push the max from the register
      g           # Take its length
                  #  i.e. 853 → 3
       ã          # Cartesian product the list that many times
                  #  i.e. [1,2,3,4,5,6,7,8,9,10] and 3
                  #   → [[1,1,1],[1,1,2],[1,1,3],...,[10,10,8],[10,10,9],[10,10,10]]
        U         # Pop and store it in variable `X`
ε              }  # Map both values of the input list:
 V                # Store the current value in variable `Y`
  Xε    }         # Map `y` over the numbers of variable `X`
    Y             # Push variable `Y`
     S            # Convert it to a list of digits
                  #  i.e. 526 → [5,2,6]
      ym          # Take each digit to the power of the current cartesian product sublist
                  #  i.e. [5,2,6] and [3,9,3] → [125,512,216]
         O        # Take the sum of each inner list
                  #  i.e. [[5,2,6],[5,2,36],[5,2,216],...,[125,512,216],...]
                  #   → [13,43,223,...,853,...]
          s       # Swap to push the (implicit) input
           N>     # Push the index + 1
                  #  i.e. 0 → 1
             è    # Index into the input-list (with automatic wraparound)
                  #  i.e. [526,853] and 1 → 853
              å   # Check if it's in the list of sums
                  #  i.e. [13,43,223,...,853,...] and 853 → 1
                P # Check if it's truthy for both both (and output implicitly)
                  #  i.e. [1,1] → 1


4

Perl 6の87の84 69バイト

nwellnhofのおかげで-15バイト!

{!grep {!grep $^b,[X+] 0,|map (*X**1..$b.msb+1),$^a.comb},.[0,1,1,0]}

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

TrueまたはFalseを返す匿名コードブロック。

説明:

{!grep {!grep $^b,[X+] 0,|map (*X**1..$b.msb+1),$^a.comb},.[0,1,1,0]}

{                                                                   }  # Anonymous code block
 !grep    # None of:
                                                          .[0,1,1,0]   # The input and the input reverse
       {!grep       # None of
                  [X+]       # All possible sums of
                       0,|   # 0 (this is to prevent single digit numbers being crossed with themself)
                          map                  ,$^a.comb   # Each digit mapped to
                              (*X**           )  # The power of
                                   1..$b.msb+1   # All of 1 to the most significant bit of b plus 1
                                                 # This could just be b+1, but time constraints...
              $^b,  # Is equal to b

@ Arnauld、A JunctionはTruthy / Falseyです。出力前にboolify演算子を使用して示しました。とにかく他の何かにゴルフをしましたが、falseとその逆の真実の値を出力できればバイトを節約できます...?
ジョーキング

説明をありがとう。真実/偽の逆転について:私はむしろノーと言いたい。
アーナルド


2

J、56バイト

h~*h=.4 :'x e.+/|:>,{x 4 :''<y&*^:(x&>)^:a:y''"+"."+":y'

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

入れ子になった明示的な定義!

使い方

powers =. 4 :'<y&*^:(x&>)^:a:y'  Explicit aux verb. x = target, y = digit
                             y   Starting from y,
               y&*^:     ^:a:    collect all results of multiplying y
                    (x&>)        until the result is at least x
              <                  Box it.

h=.4 :'x e.+/|:>,{x powers"+"."+":y'  Explicit aux verb. x, y = two input numbers
                            "."+":y   Digits of y
                  x powers"+          Collect powers of digits of y under x
                 {            Cartesian product of each item
           +/|:>,             Format correctly and compute the sums
       x e.                   Does x appear in the list of sums?

h~*h  Tacit main verb. x, y = two input numbers
      Since h tests the condition in only one direction,
      test again the other way around (~) and take the AND.


1

J、68バイト

ここでJはかなりうまくいくと思っていたが、最終的には予想以上にタフになり、さらにゴルフをするための提案が好きになるだろう...

g=.#@#:@[
1 1-:[:(([:+./[=1#.]^"#.1+g#.inv[:i.g^#@])"."0@":)/"1],:|.

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

注:f=.メイン関数ではカウントしないため、TIOカウントから3文字を減算します

食べない

1 1 -: [: (([: +./ [ = 1 #. ] ^"#. 1 + g #.inv [: i. g ^ #@]) "."0@":)/"1 ] ,: |.
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.