12
\$\begingroup\$

Given two numbers \$x,y > 2, x≠y \$ output all integers \$m\$ such that $$ x + y \equiv x \cdot y \pmod m $$ $$ x \cdot y > m > 2 $$

Input

Two integers

Output

A list of integers

Test cases

3, 4 -> 5
5, 8 -> 3, 9, 27
29, 9 -> 223
26, 4 -> 37, 74
13, 11 -> 7, 17, 119
6258, 571 -> 463, 7703, 3566489
\$\endgroup\$
2
  • \$\begingroup\$ Must we be able to accept input in any order or can we require a specific order? \$\endgroup\$
    – Shaggy
    Feb 4 at 20:20
  • \$\begingroup\$ @Shaggy you have to specify how you take input but other than that, its up to you \$\endgroup\$
    – pacman256
    Feb 4 at 20:33

20 Answers 20

9
\$\begingroup\$

05AB1E, 8 7 bytes

<P<Ñʒ2›

Attempt This Online! or try all testcases.

-1 thanks to Kevin Cruijssen

Lists the divisors of \$xy-x-y\$ greater than \$2\$.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ If you take the input as a pair, the *α¹- can be <P< for -1 byte. (And the ʒ2› has some equal-bytes alternatives, like ¦2K, 2LK or ć·K, but I haven't found anything shorter.) \$\endgroup\$ Feb 2 at 13:32
5
\$\begingroup\$

MathGolf, 8 bytes

(ε*(─2╒-

Try it online.

Explanation:

(        # Decrease both values in the (implicit) input-pair: [x-1,y-1]
 ε*      # Multiply them together: (x-1)*(y-1)
   (     # Decrease that by 1 as well: (x-1)*(y-1)-1
    ─    # Get all divisors
     2╒  # Push [1,2]
       - # Remove those values from the divisors-list
         # (after which the entire stack is output implicitly as result)
\$\endgroup\$
5
\$\begingroup\$

Thunno 2, 7 bytes

×__Fæ2>

Attempt This Online!

Based on Command Master’s idea, implemented a bit differently

×        # Multiply.          X * Y
 _       # Swapped subtract.  X * Y - X
  _      # Swapped subtract.  X * Y - X - Y
   F     # Factors.
    æ    # Filter by:
     2>  #   Greater than 2?

I chose Thunno 2 because of that swapped subtraction operator. It's the only stack golflang which has swapped versions of arithmetic operations, as far as I know.

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

Raku (Perl 6) (rakudo), 41 bytes

{[3..($^x*$^y)].grep: 0==($x+$y-$x*$y)%*}

Attempt This Online!

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

Python, 52 bytes

lambda x,y:[i for i in range(3,x*y)if(x+y)%i==x*y%i]

Attempt This Online!

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

Nekomata, 8 bytes

ᵋ*+-Ď~2>

Attempt This Online!

ᵋ*+-Ď~2>
ᵋ*          x * y
  +         x + y
   -        minus
    Ď       divisors
     ~      choose an element from the list
      2>    check if it is greater than 2
\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog APL), 22 17 16 bytes

-5 bytes thanks to @ovs
-1 bytes thanks to @att

(2↓2∪∘⍸0=⍳|⊢)×-+

Attempt This Online!

(2↓2∪∘⍸0=⍳|⊢)×-+­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢​‎⁠⁠‎⁡⁠⁣⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁢⁤​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌⁣⁡​‎‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏‏​⁡⁠⁡‌­
(2↓2∪∘⍸0=⍳|⊢)     ⍝ ‎⁡monadic function to get factors greater than 2:
           ⊢      ⍝ ‎⁢  arg
          |       ⍝ ‎⁣      mod
         ⍳        ⍝ ‎⁤          each number in range [1;arg]
       0=         ⍝ ‎⁢⁡  boolean vector, marking where result is 0
      ⍸           ⍝ ‎⁢⁢  ‎⁢⁢get indices of 1s
   2∪             ⍝ ‎⁢⁣  prepend 2 if not in array
 2↓               ⍝ ‎⁢⁤  drop first two elements (i.e. 1 and 2)
             ×-+  ⍝ ‎⁣⁡pass (⍺ × ⍵) - (⍺ + ⍵) to the function
💎

Created with the help of Luminespire.

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

C (gcc), 56 bytes

i;f(x,y){for(i=x*y;--i>2;)(x*y-x-y)%i?:printf("%d ",i);}

Try it online!

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

Google Sheets, 58 bytes

=let(p,A1*B1,s,sequence(1,p,3),filter(s,0=mod(p-A1-B1,s)))

Put \$x\$ in cell A1, \$y\$ in cell B1 and the formula in cell C1.

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

Haskell, 38 35 bytes

-3 bytes thanks to AZTECCO.

x#y=[m|m<-[3..x*y],1>mod(x*y-x-y)m]

Try it online!

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

Go, 80 bytes

func(x,y int)(M[]int){for m:=3;m<x*y;m++{if(x*y-x-y)%m<1{M=append(M,m)}}
return}

Attempt This Online!

Uses the factors of \$xy-x-y \gt 2\$ method.

Go, 81 bytes

func(x,y int)(M[]int){for m:=3;m<x*y;m++{if(x+y)%m==x*y%m{M=append(M,m)}}
return}

Attempt This Online!

Formula as verbatim, \$xy \equiv x+y \pmod m\$.

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

Charcoal, 19 bytes

F²⊞υNIΦ…³Πυ¬﹪⁻ΠυΣυι

Try it online! Link is to verbose version of code. Explanation:

F²⊞υN

Input x and y and push them to the predefined empty list.

IΦ…³Πυ¬﹪⁻ΠυΣυι

Output all numbers between 3 and the product of the list that are factors of the difference between the product and the sum.

21 bytes for a faster version that can handle the last test case in a more reasonable amount of time:

F²⊞υNI⁺³⌕A﹪⁻ΠυΣυ…³Πυ⁰

Try it online! Link is to verbose version of code. Explanation: Vectorises the remainder check and then finds the positions of the zero remainders.

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

R, 32 bytes

\(x,y,a=x*y-x-y)which(!a%%3:a)+2

Attempt This Online!

Port of Command Master's 05AB1E answer.

R, 35 bytes

\(x,y,M=3:(p=x*y))M[p%%M==(x+y)%%M]

Attempt This Online!

Straightforward approach.

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

Uiua SBCS, 19 15 bytes

▽=⊃∩◿∘,:↘3⇡.⊃×+

Try it!

-4 bytes thanks to Tbw

\$\endgroup\$
2
  • \$\begingroup\$ 15 bytes uiua.org/… \$\endgroup\$
    – Tbw
    Feb 2 at 21:07
  • \$\begingroup\$ @Tbw Nice, thanks! \$\endgroup\$
    – chunes
    Feb 2 at 21:11
2
\$\begingroup\$

Wolfram Language (Mathematica), 29 bytes

Divisors[##-##]/. 1|2->Set@$&

Try it online!

Input [x, y]. ##-## is not zero - it computes the sum minus the product of arguments.

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

JavaScript (V8), 46 bytes

x=>y=>{for(m=x*y,q=m-x-y;--m>2;)q%m||print(m)}

Try it online!


JavaScript (ES6), 52 bytes

(x,m=2)=>g=y=>++m<x*y?((x*y-x-y)%m?"":m+" ")+g(y):""

Try it online!

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

TI-BASIC, 25 bytes

For(M,3,prod(Ans)-1
If not(fPart(M⁻¹min(ΔList(Ans
Disp M
End

Takes input in Ans as a list of two integers. Add a ) to the end of the first line to make it faster.

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

Jelly, 8 bytes

’P’ÆD>Ƈ2

Try it online!

Port of Command Master’s 05AB1E answer.

-1 byte and a bugfix thanks to @Bubbler

Explanation:

’P’ÆD>Ƈ2­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌­
’         # ‎⁡Decrement both values in the list: [x - 1, y - 1]
 P        # ‎⁢Product of the list: (x - 1) * (y - 1)
  ’       # ‎⁣Decrement again: ((x - 1) * (y - 1)) - 1
   ÆD     # ‎⁤Get the divisors
      Ƈ   # ‎⁢⁡Keep items that satisfy the condition below
     > 2  # ‎⁢⁢Greater than 2
💎

Created with the help of Luminespire.

\$\endgroup\$
2
  • \$\begingroup\$ ÆḌ should be ÆD I think? Also >2$Ƈ­⁡​‎‎ can be >Ƈ2­⁡​‎‎. \$\endgroup\$
    – Bubbler
    Feb 4 at 23:51
  • \$\begingroup\$ @Bubbler fixed. \$\endgroup\$
    – Fmbalbuena
    Feb 5 at 18:58
1
\$\begingroup\$

Retina 0.8.2, 50 bytes

.+
$*
1(?=1+¶1(1+))
$1
M!&`(111+)\b(?<=^11\1+)
%`1

Try it online! Link is to test suite for faster test cases that splits input on non-digits and spaces the outputs apart. Explanation:

.+
$*

Convert to unary.

1(?=1+¶1(1+))
$1

Calculate 1+(x-1)(y-1), as that's golfier than trying to subtract x or y afterwards.

M!&`(111+)\b(?<=^11\1+)

Subtract 2 and find all factors that are greater than 2.

%`1

Convert to decimal.

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

PowerShell Core, 45 bytes

param($a,$b)3..($p=$a*$b)|?{!(($a+$b-$p)%$_)}

Try it online!

Saved two bytes using rajashekar's method, upvote their answer!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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