Karşılıklı taklitler


17

Let A oluşan bir pozitif tam sayı n ondalık hane d1,d2,...,dn . Let B başka pozitif tam sayı olması.

Bu meydan okuma amaçla, diyoruz A bir kopyacı bir B pozitif tamsayılar en az bir liste mevcutsa p1,p2,...,pn öyle ki:

i=1ndipi=B

A veB olarak adlandırılankarşılıklı copycatseğerA bir taklitçiB veB bir taklitçi A .

Misal

526 ve853 karşılıklı kopya kedilerdir çünkü:

53+29+63=853

ve:

83+51+32=526

Meydan okuma

İki A ve B pozitif tamsayısı verildiğinde , göreviniz A ve B karşılıklı kopyalarsa veya yanlış bir değerse doğruluk değeri yazdırmak veya döndürmektir .

Açıklamalar ve kurallar

  • Sen sürebilir A ve B herhangi makul, kesin biçimde (örneğin tamsayılar, dizeleri, basamak listeleri, ...)
  • A veB eşit olabilir. Bir sayı kendisinin karşılıklı bir taklitçisiyse,A007532'yeaittir.
  • Doğruluk / yanlış değerler yerine iki farklı tutarlı değer döndürebilirsiniz .
  • İçin 1A<1000 ve 1B<1000 , kodunuz içinde tamamlaması gerekir bir dakikadan daha az . Daha yüksek değerler için çok fazla zaman gerekiyorsa, bunları teoride çözebilmelidir.
  • Bu .

Test senaryoları

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

Önerilen durum: 17 2401 -> false. Neredeyse buna takıldım.
Shieru Asakoto

Yanıtlar:


8

Brachylog , 19 bayt

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

Çevrimiçi deneyin!

Çıktılar true.veyafalse.

açıklama

ẹ                     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 bayt maliyetle düzeltildi. Çünkü başarısız 2401bulunan bir 0kontrol ettim yolu ile çalışma yoktu ki I(ben hem eşledikten çünkü kesinlikle olumlu oldu Ive basamaklı bayt kaydetmek için)
Fatalize

6

Kabuk , 17 bayt

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

Çevrimiçi deneyin! 1000'in altındaki tüm test senaryolarını yaklaşık 11 saniyede tamamlar.

açıklama

Λλ€⁰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.

Neden çalışıyor?

B=d1p1++dnpn where the di are digits and pi are positive integers, then dipiB for all i, or equivalently pilogdiB. We can ignore the case di1, since exponentiating 0 or 1 does not change it. In my program the search space is 1piB (to comply with the time restriction; I would use 1piB otherwise), so if we have logdiBB, then everything is fine. If di3, this holds for all natural numbers B, so the only dangerous case is di=2. We have log2B>B only for B=8. In this case 23=8, but the search only considers exponents 1 and 2. If the other number number A contains the digit 2, either it has other nonzero digits as well (so the exponent of 2 cannot be 3 in the sum), or A=210k for some k. In the latter case, A is not a power of 8, so it cannot be a copycat of B anyway, and the program correctly returns a falsy value regardless of the other computation.


Great answer which makes me want to learn Husk. Two questions: 1. the implicit argument is mentioned again after you introduce it. When is it used? 2. Could you elaborate on why this algorithm is equivalent to the one posed in the OP?
Jonah

1
@Jonah 1. The digit function d takes the implicit argument. I clarified this in the explanation. 2. I added an argument for the program's correctness.
Zgarb

Thank you... btw, the part that had confused me was "where does the list of all ones come from?".... rereading i now realize this is merely because all the powers of 1 are just one....
Jonah


4

05AB1E, 26 22 bytes

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

Takes the input as a list (i.e. [526,853]).

Try it online or verify most test cases in the range [1,999].

Similar as my old answer below, except that the [1,n] list is hardcoded to [1,100], and it creates the cartesian list twice, once for each input-mapping, which is the main bottleneck in terms of performance.


Old 26 bytes answer that's better for performance:

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

In this version I traded in some bytes to make the performance a lot better so it can run [1,1000] with ease. Test cases containing numbers in the range [1,9999] are done in about a second on TIO. Test cases in the range [10000,99999] in about 10-15 seconds on TIO. Above that it will timeout.

Try it online or verify all test cases with numbers in the range [1,9999].

Explanation:

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 bytes

-15 bytes thanks to nwellnhof!

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

Try it online!

Anonymous code block that returns True or False.

Explanation:

{!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 is Truthy/Falsey, as I've shown by using the boolify operator before outputting. I golfed it to something else anyway, though I could save a byte if I could output a truthy value for false and vice-versa...?
Jo King

Thanks for the clarification. About the truthy/falsy inversion: I'd rather say no.
Arnauld


2

J, 56 bytes

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

Try it online!

Yay, nested explicit definition!

How it works

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 bytes

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

Try it online!

-4 bytes, thanks to Vedant Kandoi


>0 can be removed. not a:a<1. b==0:b<1
Vedant Kandoi

@VedantKandoi Thanks, though b<0 doesn't work
TFeld

1

J, 68 bytes

I thought J would perform quite well here, but it ended up being tougher than I expected and would love any suggestions for further golfing...

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

Try it online!

NOTE: we subtract 3 chars from the TIO count there since f=. on the main function doesn't count

ungolfed

1 1 -: [: (([: +./ [ = 1 #. ] ^"#. 1 + g #.inv [: i. g ^ #@]) "."0@":)/"1 ] ,: |.
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.