# Find the absolute minimum difference between 2 divided numbers from n [duplicate]

## Input:

An integer n

## Output:

A string A * B

## Example

12

Possible 2 divisible numbers: 12 * 1, 6 * 2, 4 * 3

12-1 = 11

6-2 = 4

4-3 = 1


4 and 3 has the minimal absolute difference of 1

Therefore, Output:

4 * 3

Order of the numbers does not matter

This challenge idea was taken from a puzzle in Clash of Code in https://www.codingame.com

This is , so shortest answer in bytes wins!

• your sandbox post? Dec 27, 2021 at 2:00
• I would personally recommend against that because it does not really add to the challenge in terms of making it interesting, and detracts from the core of the challenge. Of course it's up to you but that is just my personal advice based on typical I/O formats and conventions. Dec 27, 2021 at 2:10
• Related: Find the Squarish Root
– xnor
Dec 27, 2021 at 2:11
• Is the input guaranteed to be a positive integer, or can it be negative? Dec 27, 2021 at 2:45
• Is the space around star sign required?
– tsh
Dec 27, 2021 at 2:49

# Python 2, 74 bytes

def f(x):q=[i for i in range(1,1+x)if x%i<1];q=q[len(q)/2];print q,"*",x/q


Try it online!

• is it possible to remove the function? sry i have not tried python 2 yet only python3x Dec 27, 2021 at 2:29
• @DialFrost this is 1 byte longer Dec 27, 2021 at 2:41

# Python 2, 55 bytes

f=lambda n,m=1:n%m|(m*m<n)and f(n,m+1)orn/m+" * "+m

Attempt This Online!

# Vyxal, 11 bytes

KḂZµƒε;h×YṄ


Try it Online!

-2 thanks to lyxal

K           # Factors
ḂZ         # Zipped with reverse
µ  ;h    # Minimum by
ƒε      # Reduced by abs. diff.
×Y  # Interleave with '*'
Ṅ # Join by spaces

• Try it Online! for 11 Dec 27, 2021 at 2:10
• @lyxal Thanks!! Dec 27, 2021 at 2:14

# Jelly, 13 bytes

ÆDżU$ạ/ÞḢj”*K  Try It Online! -1 byte thanks to a golf from lyxal ported by emanresu A -1 byte thanks to emanresu A ÆDżU$ạ/ÞḢj”*K  Main Link
ÆD             Divisor List
żU\$          Interleaved with its reverse
Þ       Sorted by
ạ/        Absolute Difference
Ḣ      Get the first one
j”*   Join on "*"
K  Join on spaces

• Could you split the code out and explain like how emanresu A did? (Jelly is hard to read XD) Dec 27, 2021 at 2:09
• @emanresuA oh clever Dec 27, 2021 at 2:18
• -1 more 'cos sort's shorter Dec 27, 2021 at 2:18

# Python, 104 bytes

lambda n:'%d * %d'%min([(x,n/x)for x in[i for i in range(1,n)if(n/i)%1==0]],key=lambda x:abs(x-x))


Try it Online!

# Python, 96 bytes

f=lambda x:(lambda y:str(z:=y[len(y)//2])+" * "+str(x//z))([i for i in range(1,x) if x/i==x//i])

• what python is this? please specify Dec 27, 2021 at 2:28
• @DialFrost It's Python 3 as you can tell by // and := Dec 27, 2021 at 2:31
• if im right there is no such character as := in python3 and please try to use tio.run to show that it works thanks Dec 27, 2021 at 2:33
• @DialFrost there is in python 3.8+. here's a TIO link: tio.run/… Dec 27, 2021 at 2:41
• you can save 2 bytes by dropping the f= at the top (put f=\ into the header for testing) since we allow anonymous function submissions Dec 27, 2021 at 2:42

# JavaScript (Node.js), 52 bytes

f=(n,i=n**.5|(-n)**.5)=>n%i?f(n,i+1):i+' * '+(n/i|0)


Try it online!

# JavaScript (Node.js), 41 bytes

f=(n,i=n**.5|0)=>n%i?f(n,i+1):i+' * '+n/i


Try it online!

For positive number only.

# Factor + math.primes.factors, 73 bytes

[ divisors dup reverse zip [ first2 - abs ] infimum-by "%d * %d"vprintf ]


Try it online!

                            ! 12
divisors                    ! { 1 2 3 4 6 12 }
dup                         ! { 1 2 3 4 6 12 } { 1 2 3 4 6 12 }
reverse                     ! { 1 2 3 4 6 12 } { 12 6 4 3 2 1 }
zip                         ! { { 1 12 } { 2 6 } { 3 4 } { 4 3 } { 6 2 } { 12 1 } }
[ first2 - abs ] infimum-by ! { 3 4 }
"%d * %d"vprintf            !


# Pari/GP, 38 bytes

n->vecmin([abs(d-n/d)|d<-divisors(n)])


Try it online!