पारस्परिक नकलची


17

आज्ञा देना A सकारात्मक पूर्णांक है जिसमें n दशमलव अंक । चलो एक अन्य पूर्णांक सकारात्मक हो।d1,d2,...,dnB

इस चुनौती के प्रयोजन के लिए, हम कहते हैं एक नकल की अगर वहाँ धनात्मक पूर्णांक कम से कम एक सूची मौजूद है ऐसी है कि:ABp1,p2,...,pn

i=1ndipi=B

A and B are called reciprocal copycats if A is a copycat of B and B is a copycat of A.

Example

526 and 853 are reciprocal copycats because:

53+29+63=853

and:

83+51+32=526

The challenge

Given two positive integers A and B, your task is to print or return a truthy value if A and B are reciprocal copycats or a falsy value otherwise.

Clarifications and rules

  • You may take A and B in any reasonable, unambiguous format (e.g. integers, strings, lists of digits, ...)
  • A and B may be equal. If a number is a reciprocal copycat of itself, it belongs to A007532.
  • Instead of truthy/falsy values, you may return two distinct consistent values.
  • For 1A<1000 and 1B<1000, your code must complete in less than one minute. If it's taking too much time for higher values, it must however be able to solve them in theory.
  • This is .

Test cases

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

Suggested case: 17 2401 -> false. I'm almost tripped on this.
Shieru Asakoto

जवाबों:


8

Brachylog, 19 bytes

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

Try it online!

Outputs true. or false.

Explanation

ẹ                     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 Fixed at the cost of 1 byte. It failed because 2401 contained a 0 which didn't work with the way I checked that I was strictly positive (because I mapped it on both I and the digit to save bytes)
Fatalize

6

Husk, 17 bytes

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

Try it online! Finishes all test cases under 1000 in about 11 seconds.

Explanation

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

Why it works

If we have 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 ] ,: |.
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.