17
\$\begingroup\$

Intro

Two numbers are a reversed multiple pair if they satisfy the following property:

$$ a\cdot b = \operatorname{reversed}( (a-1)\cdot b ) $$

Here, \$\operatorname{reversed}()\$ means to reverse the digits of a number (e. g 123 becomes 321.)

Example

$$ a=6,\quad b=9,\quad 6\cdot 9=54,\quad 5\cdot 9=45 $$

As you can see, \$45\$ is the reversed version of \$54\$.

Task

Given two integers, \$a\$ and \$b\$, output if these two numbers is a reversed multiple pair.

Test Cases

6 9 => True
4 5 => False
3 2178 => True
34 2079 => True
7 8 => False
600 100 => False
46 1089 => True
817 503 => False
6 9009 => True
321 81 => False
10 1089 => False

Shortest code wins!

\$\endgroup\$
8
  • \$\begingroup\$ Suggest test case: 321 81 => False. An answer checking b for divisibility with 9 will pass all existing test cases but not this one. \$\endgroup\$
    – chunes
    Jan 29 at 21:43
  • 2
    \$\begingroup\$ Is 10 1089 truthy or falsy? Currently, different answers don't agree on this testcase. \$\endgroup\$
    – tsh
    Jan 30 at 2:55
  • 2
    \$\begingroup\$ May I assume inputs are positive? Or what is expected output for -5 9? \$\endgroup\$
    – tsh
    Jan 30 at 6:53
  • 1
    \$\begingroup\$ Shouldn’t 10 1089 be false? \$\endgroup\$
    – tsh
    Jan 30 at 13:03
  • 3
    \$\begingroup\$ reverse(9*1089)=reverse(9801)=1089≠10890=10*1089 \$\endgroup\$
    – tsh
    Jan 30 at 14:22

28 Answers 28

10
+50
\$\begingroup\$

tinylisp, 76 bytes

(load library
(d f(q((a b)(e(to-base 10(*(- a 1)b))(reverse(to-base 10(* a b

Try it online!

\$\endgroup\$
8
\$\begingroup\$

Brachylog, 8 bytes

-₁ʰ×↔~×?

Try it online!

Explanation

           Input = [A, B]
-₁ʰ        Compute [A-1, B]
   ×       Compute (A-1) × B
    ↔      Reverse digits
     ~×?   It can be unmultiplied into [A,B]
\$\endgroup\$
6
\$\begingroup\$

Python 3, 37 bytes

lambda a,b:str(a*b)==str(~-a*b)[::-1]

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Jelly, 9 bytes

,’$×DU⁼¥/

Try It Online!

,’$×DU⁼¥/    Main Link; dyad accepting a, b
,’$          pair: [a, a - 1]
   ×         multiply: [ab, (a - 1)b]
    D        to digit list
        /    reduce between the two:
     U       is the reverse of the former
      ⁼      equal to the latter?
\$\endgroup\$
5
\$\begingroup\$

Jelly, 6 bytes

’×ṚḌ⁼×

A dyadic Link accepting \$a\$ and \$b\$ that yields 1 if \$a\times b = \text{reversed}( (a - 1)\times b )\$, 0 if not.

Try it online! Or see the test-suite.

How?

’×ṚḌ⁼× - Link: a, b
’      - a-1
 ×     - (a-1) times b
  Ṛ    - reverse the digits of (a-1)×b
   Ḍ   - convert back to an integer
     × - a×b
    ⁼  - equal?
\$\endgroup\$
3
  • \$\begingroup\$ how do you code in jelly? \$\endgroup\$
    – DialFrost
    Jan 30 at 4:05
  • 3
    \$\begingroup\$ @DialFrost the same way you code in other languages:P \$\endgroup\$
    – PyGamer0
    Jan 30 at 4:43
  • 1
    \$\begingroup\$ @DialFrost see the tutorial to get started. \$\endgroup\$ Jan 30 at 13:19
5
\$\begingroup\$

APL (Dyalog Unicode), 9 bytes

Anonymous tacit infix function, taking \$a\$ as left argument and \$b\$ as right argument.

×≡∘⌽⍥⍕×-⊢

Try it online! (Uses Extended because TIO hasn't updated to 18.0)

× [Does] the product

≡∘ match the…

⌽⍥ reversed, when both are…

   stringified

× product

- minus

 the right argument[?]

\$\endgroup\$
4
\$\begingroup\$

Factor, 47 39 bytes

[ over * dup present reverse dec> - = ]

Try it online!

Saved 8 bytes thanks to @ovs' observations.

Explanation

Takes input as b a.

         ! 9 6
over     ! 9 6 9
*        ! 9 54
dup      ! 9 54 54
present  ! 9 54 "54"
reverse  ! 9 54 "45"
dec>     ! 9 54 45
-        ! 9 9
=        ! t
\$\endgroup\$
4
\$\begingroup\$

Python 2, 31 bytes

lambda a,b:`a*b`==`a*b-b`[::-1]

Using the logic that $$ ab = \operatorname{rev}( (a-1)b ) \iff ab = \operatorname{rev}(ab-b) $$ -2 thx to @dingledooper

Try it online!

Other solutions:

Python 2, 31 bytes

lambda a,b:`a*b`[::-1]==`a*b-b`

Using the logic that $$ \operatorname{rev}(ab) = (a-1)b \iff \operatorname{rev}(ab) = ab-b $$ -2 thx to @dingledooper

Try it online!

Python 2, 34 bytes

lambda a,b:a*b-int(`a*b`[::-1])==b

Using the logic that $$ ab - \operatorname{rev}(ab) =b $$

Try it online!

^Credits to @ovs for the equations!

\$\endgroup\$
2
  • 3
    \$\begingroup\$ (a-1)*b -> a*b-b \$\endgroup\$ Jan 30 at 3:47
  • \$\begingroup\$ thx @dingledooper! im so dumb i didnt see that \$\endgroup\$
    – DialFrost
    Jan 30 at 3:55
3
\$\begingroup\$

Vyxal, 7 bytes

:‹"*÷Ṙ=

Try it Online!

  "     # Pair the first input with
:‹      # Itself decremented
   *    # Multiply both by the second input
    ÷   # Push both to the stack
     Ṙ= # Is one reversed equal to the other?
\$\endgroup\$
3
\$\begingroup\$

Charcoal, 13 bytes

NθNη⁼×θη⮌×⊖θη

Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - for a reversed pair, nothing if not. Explanation:

Nθ              First input as a number
  Nη            Second input as a number
      θ         First input
     ×          Multiplied by
       η        Second input
    ⁼           Equals
           θ    First input
          ⊖     Decremented
         ×      Multiplied by
            η   Second input
        ⮌       Reversed
                Implicitly print
\$\endgroup\$
3
\$\begingroup\$

Pari/GP, 40 bytes

f(a,b)=d=digits;d(a*b)==Vecrev(d(a--*b))

Try it online!

\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 41 bytes

Expects (a)(b). Returns a Boolean value.

a=>b=>[...a*b+''].reverse().join``==a*b-b

Try it online!

\$\endgroup\$
3
\$\begingroup\$

C (gcc), 55 bytes

c;r;f(a,b){for(r=0,c=~-a*b;c;c/=10)r+=9*r+c%10;r-=a*b;}

Try it online!

Inputs two integers \$a\$ and \$b\$.
Returns a falsy value if these two numbers are a reversed multiple pair or a truthy value otherwise.

\$\endgroup\$
3
\$\begingroup\$

Ruby, 33 bytes

->a,b{"#{a*=b}"==(a-b).digits*''}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Haskell, 43 33 bytes

a?b=a*b==read(reverse$show$a*b-b)

Try it online!

  • I haven't thought reverse was available in prelude, no reason to use foldl(flip(:))""String
\$\endgroup\$
2
\$\begingroup\$

Retina 0.8.2, 55 bytes

\d+
$*
1,
,
1(?=.*,(1+))
$1
,
,$`
1+
$.&
+`(.),\1
,
^,$

Try it online! Link includes test cases. Explanation:

\d+
$*

Convert to unary.

1,
,

Decrement a.

1(?=.*,(1+))
$1

Multiply a-1 by b.

,
,$`

Add that to b giving (a-1)b,ab.

1+
$.&

Convert to decimal.

+`(.),\1
,
^,$

Test whether the results are mirror images of each other.

\$\endgroup\$
2
\$\begingroup\$

Red, 47 45 bytes

func[a b][(to""a * b)= reverse to""a - 1 * b]

Try it online!

\$\endgroup\$
2
\$\begingroup\$

BQN, 33 22 Bytes

This seems too long.

A little better.

{(𝕨×𝕩)=•BQN⌽•Fmt𝕩×𝕨-1}

Try it!

¯11 bytes thanks to @Razetime

\$\endgroup\$
2
\$\begingroup\$

APL+WIN, 22 20 bytes

Prompts for a then b. Minus 2 bytes using rearrangement logic

(⍎⌽⍕n-b)=n←(b←⎕)×a←⎕

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
2
\$\begingroup\$

Retina, 41 bytes

L$`.+,
*$'*_,$($^$.(*$'*))*_$'*
^(_+),\1$

Try it online! Link includes test cases. Explanation: Based on @ovs' 05AB1E answer.

L$`.+,

Match a as $& (Retina ignores the trailing comma when performing arithmetic) and b as $' and replace the whole input with the result.

*$'*_,$($^$.(*$'*))*_$'*

Calculate a*b and also rev(a*b)+b in unary.

^(_+),\1$

See whether they are the same.

\$\endgroup\$
2
\$\begingroup\$

Burlesque, 14 bytes

Jpdjp^-..*<-==

Try it online!

Takes input as array of pairs {a b}

J   # Duplicate
pd  # Product of array
j   # Swap
p^  # Push elements to stack (b, a)
-.  # Decrement
.*  # Product
<-  # Reverse digits
==  # Equal
\$\endgroup\$
2
\$\begingroup\$

GeoGebra, 86 77 70 bytes

a=6
b=9
s=Text(ab-b)
o=Sum(Zip(Take(s,c,c),c,Length(s)…1))==Text(ab)

Try It On GeoGebra!

Input is a and b, output is o as true if a and b are a reversed multiple pair, false otherwise.

The example input shown in the code is a=6 and b=9, but you can change these values to whatever you want.

\$\endgroup\$
2
\$\begingroup\$

J, 20 14 bytes

*=|.&.":@(*<:)

-6 bytes, thanks @Jonah

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Taking args in reverse order, *=|.&.":@(*<:) for 14 bytes: Try it online! \$\endgroup\$
    – Jonah
    Feb 24 at 4:04
1
\$\begingroup\$

PHP, 79 bytes

$a=trim(fgets(STDIN));$b=trim(fgets(STDIN));echo $a*$b==(int)strrev($b*($a-1));

Try it online!

With the help of Github Copilot

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Instead of all of this, you can just do fn($a,$b)=>$a*$b==+strrev($b*($a-1));. This creates an anonymous function and does the casting to a number in fewer bytes. This syntax was introduced in PHP 8.0. You can see it working for yourself here: sandbox.onlinephpfunctions.com/code/… \$\endgroup\$ Jan 31 at 17:41
1
\$\begingroup\$

MathGolf, 7 bytes

`(*x¬*=

Try it online.

Explanation:

`        # Duplicate the top two items, using the implicit inputs
         #  STACK: b,a,b,a
 (       # Decrease the top item
         #  STACK: b,a,b,a-1
  *      # Multiply the top two together
         #  STACK: b,a,b*(a-1)
   x     # Reverse the top integer
         #  STACK: b,a,reverse(b*(a-1))
    ¬    # Reverse rotate the stack:
         #  STACK: reverse(b*a(-1)),b,a
     *   # Multiply the top two again
         #  STACK: reverse(b*a(-1)),b*a
      =  # Check if the two are equal
         #  STACK: reverse(b*a(-1))==b*a
         # (after which the entire stack joined together is output implicitly)
\$\endgroup\$
1
\$\begingroup\$

T-SQL, 31 bytes

Using bitwise operators

PRINT-1/~(@*@b^reverse(@b*~-@))

Try it online

\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 56 bytes

(a,b,d=digits,s=string)->s(d(a*b))==s(reverse(d(a*b-b)))

Try it online!

Pretty straightforward answer, with nothing particularly clever to it (unfortunately).

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 33 bytes: a+b="$(a*b)"==reverse("$(a*b-b)"). \$\endgroup\$ Feb 1 at 23:38
  • \$\begingroup\$ @dingledooper I always forget about the operator trick 🤦 That seems different enough to merit a separate answer! \$\endgroup\$
    – Sundar R
    Feb 1 at 23:43
1
\$\begingroup\$

Ly, 15 bytes

nn&s*SrJlf,*=u;

Try it online!

nn               - read two numbers from STDIN
  &s             - save the entries to the backup cell
    *            - multiple the two numbers passed in
     S           - convert integer to individual digits on stack
      r          - reverse stack
       J         - re-compose digits on stack into a integer
        l        - restore saved numbers
         f       - flip top two entries
          ,      - decrement the top of stack
           *     - multiple top two entries
            =    - compare the two entries on the stack
             u   - print "1|0" from comparison
              ;  - exit the program
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.