3
\$\begingroup\$

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!

\$\endgroup\$
10
  • 1
    \$\begingroup\$ your sandbox post? \$\endgroup\$
    – Fmbalbuena
    Dec 27, 2021 at 2:00
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – hyper-neutrino
    Dec 27, 2021 at 2:10
  • 1
    \$\begingroup\$ Related: Find the Squarish Root \$\endgroup\$
    – xnor
    Dec 27, 2021 at 2:11
  • 2
    \$\begingroup\$ Is the input guaranteed to be a positive integer, or can it be negative? \$\endgroup\$ Dec 27, 2021 at 2:45
  • 2
    \$\begingroup\$ Is the space around star sign required? \$\endgroup\$
    – tsh
    Dec 27, 2021 at 2:49

9 Answers 9

3
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ is it possible to remove the function? sry i have not tried python 2 yet only python3x \$\endgroup\$
    – DialFrost
    Dec 27, 2021 at 2:29
  • \$\begingroup\$ @DialFrost this is 1 byte longer \$\endgroup\$
    – hyper-neutrino
    Dec 27, 2021 at 2:41
3
\$\begingroup\$

Python 2, 55 bytes

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

Attempt This Online!

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

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
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Try it Online! for 11 \$\endgroup\$
    – lyxal
    Dec 27, 2021 at 2:10
  • \$\begingroup\$ @lyxal Thanks!! \$\endgroup\$
    – emanresu A
    Dec 27, 2021 at 2:14
2
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ Could you split the code out and explain like how emanresu A did? (Jelly is hard to read XD) \$\endgroup\$
    – DialFrost
    Dec 27, 2021 at 2:09
  • \$\begingroup\$ @emanresuA oh clever \$\endgroup\$
    – hyper-neutrino
    Dec 27, 2021 at 2:18
  • \$\begingroup\$ -1 more 'cos sort's shorter \$\endgroup\$
    – emanresu A
    Dec 27, 2021 at 2:18
1
\$\begingroup\$

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[0]-x[1]))

Try it Online!

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

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])
\$\endgroup\$
5
  • \$\begingroup\$ what python is this? please specify \$\endgroup\$
    – DialFrost
    Dec 27, 2021 at 2:28
  • \$\begingroup\$ @DialFrost It's Python 3 as you can tell by // and := \$\endgroup\$
    – emanresu A
    Dec 27, 2021 at 2:31
  • \$\begingroup\$ if im right there is no such character as := in python3 and please try to use tio.run to show that it works thanks \$\endgroup\$
    – DialFrost
    Dec 27, 2021 at 2:33
  • \$\begingroup\$ @DialFrost there is in python 3.8+. here's a TIO link: tio.run/… \$\endgroup\$
    – hyper-neutrino
    Dec 27, 2021 at 2:41
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – hyper-neutrino
    Dec 27, 2021 at 2:42
0
\$\begingroup\$

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.

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

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            !
\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 38 bytes

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

Try it online!

\$\endgroup\$

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