相互模仿


17

A为由n十进制数字d1,d2,...,dn组成的正整数d n。让B为另一个正整数。

对于这一挑战的目的,我们称之为A一个山寨B如果存在正整数中的至少一个列表p1,p2,...,pn使得:

i=1ndipi=B

AB被称为倒数仿冒如果A是的模仿BB是的模仿A

526853是互惠的模仿者,因为:

53+29+63=853

和:

83+51+32=526

挑战

给定两个正整数AB,您的任务是打印或返回真实值,如果AB是倒数模仿者,否则返回假值。

澄清和规则

  • 您可以采用任何合理,明确的格式(例如整数,字符串,数字列表等)来获取AB
  • AB可以相等。如果数字是其自身的对等模仿,则它属于A007532
  • 代替真/假值,您可以返回两个不同的一致值。
  • 对于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。我差点被绊倒了。
Shieru Asakoto

Answers:


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个字节。它失败了,因为其中2401包含一个0与我I严格检查是肯定的方式不符的方法(因为我将其同时映射I在数字和数字上以保存字节)
Fatalize

6

外壳,17个字节

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

在线尝试! 在大约11秒内完成1000以下的所有测试用例。

说明

Λλ€⁰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++dnpndipidipiBipilogdiBdi101个1个p一世1piBlogdiBBdi3Bdi=2log2B>BB=823=812一种223A=210kkA8 无论如何,该程序将正确地返回一个伪造的值,而不考虑其他计算。


很好的答案,这使我想学习Husk。两个问题:1.引入隐式参数后,将再次提及它。什么时候使用?2.您能否详细说明为什么该算法与OP中的算法等效?
乔纳

1
@Jonah 1. digit函数d采用隐式参数。我在解释中对此做了澄清。2.我为程序的正确性添加了一个参数。
Zgarb

谢谢...顺便说一句,让我感到困惑的那部分是“所有列表的来源是什么?”。...我现在重新读懂,这仅仅是因为1的所有幂仅是一个。
乔纳


4

05AB1E26 22 字节

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

将输入作为列表(即[526,853])。

在线尝试验证范围内的大多数测试用例[1,999]

类似于我下面的旧答案,不同之处在于该[1,n]列表被硬编码为[1,100],并且两次创建笛卡尔列表,每个输入映射都创建一次,这是性能的主要瓶颈。


旧的26个字节的答案更适合于性能:

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

在此版本中,我以字节为单位进行了交易,以使性能更好,因此可以[1,1000]轻松运行。[1,9999]在TIO上大约一秒钟即可完成包含范围内数字的测试用例。测试用例[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字节

-15字节感谢nwellnhof!

{!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,一个结点是真实的/虚假的,正如我在输出之前使用boolify运算符所显示的那样。无论如何我都会打它,尽管如果我可以输出一个真值,如果为false,反之亦然,我可以节省一个字节。
Jo King

感谢您的澄清。关于真实/虚假反转:我宁愿说不。
Arnauld


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

Python 2中149 147 143 139 132 118个 108 107 106 105字节

lambda a,b:g(a,b)*g(b,a)
g=lambda a,b:any(g(a/10,b-(a%10)**-~i)for i in(a*b>0)*range(len(bin(b))))or b==0

在线尝试!

-4字节,感谢Vedant Kandoi


>0可以删除。not aa<1b==0b<1
Vedant Kandoi

@VedantKandoi谢谢,尽管b<0没有用
TFeld

1

J,68个字节

我以为J会在这里表现不错,但最终变得比我想象的要强,并且喜欢任何关于进一步打高尔夫球的建议...

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

在线尝试!

注意:我们从TIO计数中减去3个字符,因为f=.在主函数上不计数

不打高尔夫球

1 1 -: [: (([: +./ [ = 1 #. ] ^"#. 1 + g #.inv [: i. g ^ #@]) "."0@":)/"1 ] ,: |.
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.