# The Uncommon Factor Number

Based on a chat message

## The Challenge

Given an input number $$\n > 9\$$, construct its reverse, ignoring leading zeros. Then, construct a list of all prime factors that the number and its reverse don't have in common. Multiply those factors together to create the Uncommon Factor Number of the input.

Or, to put it another way: if $$\\text{rev}(n)\$$ denotes the decimal reversal of integer $$\n\$$, calculate the product of $$\n\$$ and $$\\text{rev}(n)\$$ divided by the square of the $$\\gcd(n, \text{rev}(n))\$$.

Output that number.

### Worked examples

For example, $$\2244\$$ reverses to $$\4422\$$. The prime factors of the first are $$\[2, 2, 3, 11, 17]\$$ and the prime factors of the reverse are $$\[2, 3, 11, 67]\$$. The numbers not in common multiplicities are $$\[2, 17, 67]\$$, so $$\2278\$$ is the output.

For another example, $$\1234\$$ reverses to $$\4321\$$. The product is $$\5332114\$$ and the GCD is $$\1\$$, so the output is $$\5332114\$$.

## Further clarifications

Obviously a palindromic number will have all its factors in common with its reverse, so in such a case the output is $$\1\$$ $$\\left(\frac{n\times n}{n^2}\right)\$$. Obviously, it's also possible for the output to be the multiplication all factors (i.e., the gcd is $$\1\$$ -- the input and its reverse are co-prime), as in the case of the $$\1234\$$ example.

## Rules

• The input and output can be assumed to fit in your language's native integer type.
• The input and output can be given in any convenient format.
• Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
• If possible, please include a link to an online testing environment so other people can try out your code!
• Standard loopholes are forbidden.
• This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

## Examples

in
out

17
1207

208
41704

315
1995

23876
101222302
• Can we assume the input will not have leading zeros? Sep 8, 2017 at 14:48
• @Mr.Xcoder Huh? You mean trailing zeroes? Sep 8, 2017 at 14:50
• @EriktheOutgolfer No, leading zeros is exactly what I mean. Also Sep 8, 2017 at 14:50
• The second test case should be 1995 (I believe) Sep 8, 2017 at 14:53
• @LuisMendo Thanks. Good addition. Sep 8, 2017 at 15:14

# 05AB1E, 6 bytes

### Code

Â‚D¿÷P

Uses the 05AB1E encoding. Try it online!

### Explanation

Â‚        # Get the array [input, reversed(input)]
D       # Duplicate that array
¿      # Calculate the GCD of the array
÷     # Divide each element in the array by the GCD
P    # Product of that array
• A nice, simple alternative to the formula provided in the challenge - +1. Tried the same in Japt but it came out 2 bytes longer than what I already had. Sep 8, 2017 at 23:42

# J, 18 bytes

".@|.@":(*%*:@+.)]

Try it online!

Alternatively (credit to @Adnan's approach for the second one),

".@|.@":(*%2^~+.)]
".@|.@":*/@(,%+.)]

*/@(,%+.)|.&.":

# Explanation

This is just a straightforward implementation of the algorithm given by the OP.

".@|.@":(*%*:@+.)]
]  n (input)
".@|.@":            n reversed
*          Product of the two
%         Divided by
+.      GCD
*:         Squared

# Explanation, @miles's solution

Very clever.

*/@(,%+.)|.&.":
|.&.":  Reverse digits
&.":   Convert to string, apply next function, and undo conversion
|.       Reverse
(,%+.)        Divide n and reverse(n) by GCD of both
*/               Product
• 15 bytes with */@(,%+.)|.&.": Sep 8, 2017 at 16:57
• @miles I love the under trick
– cole
Sep 8, 2017 at 17:01
• @miles that one is really slick. Sep 8, 2017 at 18:54
• Why not submit the 15 byte version as your main solution? Sep 8, 2017 at 23:39
• @Shaggy Not sure. My inclination was to respond with "it's significantly different from my own," but it's really just two optimizations. I'll get to updating it later.
– cole
Sep 9, 2017 at 0:50

# Mathematica, 33 bytes

#(s=IntegerReverse@#)/GCD[#,s]^2&

Try it online!

# Jelly, 8 bytes

,ṚḌµ:g/P

Try it online!

• Dammit, I had exactly the same (except DU instead of ) Sep 8, 2017 at 14:54
• @ETHproductions Yeah on an integer reverses the digits but doesn't convert back to an integer. Sep 8, 2017 at 15:07

## Haskell, 44 bytes

f x|r<-read$reverse$show x=x*rdivgcd x r^2

Try it online!

# R, 108 89 bytes

-19 bytes thanks to plannapus for his gcd algorithm

function(n){k=1:nchar(n)-1
q=1:n
(r=sum(n%/%10^k%%10*10^rev(k)))*n/max(q[!r%%q&!n%%q])^2}

This will attempt to allocate at least one vector of size 4*n bytes (and I think as many as 4), so this will throw a memory error for sufficiently large n.

Try it online!

# JavaScript, 676460 59 bytes

So many bytes just to reverse the number :(

Takes input as a string. 1 byte saved thanks to l4m2.

f=(n,x=n,y=r=[...n].reverse().join)=>y?f(n,y,x%y):n*r/x/x

Try it online!

• n*r/x/x?aaaaaaaaaaa
– l4m2
Dec 19, 2022 at 5:55

# Python 3, 73 68 bytes

-5 bytes thanks to Mr. Xcoder.

import math
def f(n):g=int(str(n)[::-1]);print(n*g/math.gcd(n,g)**2)

Try it online!

• 68 bytes Sep 8, 2017 at 15:07

# MATL, 1312 11 bytes

tVPU*1MZdU/

### Explanation

t      % Imoplicit input: number. Duplicate
VPU    % String representation, flip, evaluate. This reverses digits
*      % Multiply input and reversed-digit version
1M     % Push the input and reversed-digit version again
Zd     % Greatest common divisor
U      % Square
/      % Divide. Implicit display

# Neim, 11 bytes

𝕓𝕋₁𝐫𝐕₁𝐕𝕌𝐠ᛦ𝕍

Try it online!

No GCD built-in. ;-;

# Husk, 10 bytes

S¤§÷o□⌋*i↔

Try it online!

-1 thanks to H.PWiz.
-1 thanks to Zgarb.

# Japt, 1312 11 bytes

sw
*V/yU ²

Try it

## Explanation

Implicit input of integer U. The empty line at the beginning, prevents the following line from overwriting U

sw

Convert U to a string (s), reverse it (w), convert back to an integer and assign to variable V.

*V

Multiply U by V.

/

Divide.

yU

GCD of V and U.

²

Squared. Implicit output of resulting integer.

## Alternative, 13 bytes

Just because I like being able to use N.

NpUsw)mxNry)×

Try it

• Smart trick with GCD. I think that algorithm might actually be shorter than the current Jelly solution... Sep 8, 2017 at 15:03
• @ETHproductions In Jelly GCD ends up being longer... Sep 8, 2017 at 15:08
• @EriktheOutgolfer I "have" an 8-byte version, but this involves dividing the results of two dyads and I'm not sure how to properly do that... Sep 8, 2017 at 15:34

# Pyth, 13 bytes

/*QKs_Q^iKQ2

Try it here!

# Pyth, 15 bytes

This uses Adnan's approach and takes input as a String.

KsM_BQ*Fm/diFKK

Try it here

# x86 Machine Code, 39 bytes

;;; Obtain a "reversed" version of the input value.
;;;
;;; To do this, each iteration of a loop, we take the input value modulo 10,
;;; add that to our accumulator (EDI), multiply the accumulator by 10, and
;;; divide the input value by 10. x86's DIV instruction does both modulo and
;;; division as a single operation, with the cost of clobbering two output
;;; registers (EAX and EDX). We clobber the input value throughout the loop
;;; (the way we know we're done is when it becomes 0---that means that we have
;;; pulled all of the digits off of it), so we need to save a copy of it first.
89 C8           mov    eax, ecx     ; make copy of input
31 FF           xor    edi, edi     ; clear accumulator
6A 0A           push   10
5E              pop    esi          ; set ESI to 10
Reverse:
0F AF FE        imul   edi, esi     ; accumulator *= 10
99              cdq                 ; zero EDX in preparation for division
F7 F6           div    esi          ; EDX:EAX / 10 (EAX is quot, EDX is rem)
01 D7           add    edi, edx     ; accumulator += remainder
85 C0           test   eax, eax     ; was quotient 0?
75 F4           jnz    Reverse      ; if not, keep looping and extracting digits

;;; At this point, EAX is 0 (clobbered throughout the loop),
;;; ECX still contains a copy of our original input, and
;;; EDI contains the 'reversed' input.
89 C8           mov    eax, ecx     ; make another copy of the input
F7 E7           mul    edi          ; multiply input (implicit EAX operand)
;  by 'reversed', with result in EDX:EAX
;  (note: EDX will be 0)

;;; Compute the greatest common denominator (GCD) of the input and
;;; the 'reversed' values, using a subtraction-based algorithm.
GCD_0:
39 CF           cmp    edi, ecx     ; compare the two values
72 02           jb     GCD_1        ; go to GCD_1 if less than
87 F9           xchg   ecx, edi     ; swap values
GCD_1:
29 F9           sub    ecx, edi     ; subtract
75 F6           jnz    GCD_0        ; if sum != 0, go back to the top

;;; Square the GCD.
0F AF FF        imul   edi, edi

;;; Divide the product of input and 'reversed' by the square of the GCD.
;;; Remember from above that the product of input and 'reversed' is in
;;; the EAX register, and we can assume EDX is 0, so we don't need to do
;;; a CDQ here in preparation for the division. Using EAX as the implicit
;;; source operand saves us a byte when encoding DIV.
F7 F7           div    edi

;;; The DIV instruction placed the quotient in EAX,
;;; which is what we want to return to the caller.
C3              ret

The above function computes the "uncommon factor number" of the specified input parameter. Following the register-based __fastcall calling convention, the parameter is passed in the ECX register. The result is returned in the EAX register, as with all x86 calling conventions.

Try it online!

This took an awfully long time to write in such a compact form, but it was a fun exercise. Lots of contortions to get the most optimal register scheduling possible, within the constraints of the x86 DIV instruction's implicit operands and trying to use short encodings of MUL and XCHG instructions whenever possible. I'd be very curious to see if someone can think of another way to shorten it further. My brain was quite fried by the end. Thank a compiler next time you see one! (Although this is way better code than what a compiler would generate... Especially if you tweaked it slightly without size constraints, removing things like XCHG.)

# APL (Dyalog Extended), 9 bytes

⊢(∧÷∨)⌽⍢⍕

Try it online!

$$\text{UCF}(n) = \frac{n\times \text{rev}(n)}{\gcd(n, \text{rev}(n))^2} = \frac{\text{lcm}(n,\text{rev}(n))}{\gcd(n,\text{rev}(n))} = \frac{\text{lcm}}{\gcd}(n,\text{rev}(n))$$

reverse under conversion to string, then apply ∧÷∨ (lcm ÷ gcd) between the result and and the original argument.

# Jelly, 7 bytes

ṚḌæl:gɗ

Try it online!

-1 byte thanks to ovs

Similar to Erik's Jelly answer, but uses a dyadic route rather than monadic lists.

Uses the formula found by ovs

$$\text{UCF}(n) = \frac{\text{lcm}(n,\text{rev}(n))}{\gcd(n, \text{rev}(n))}$$

## How it works

ṚḌæl:gɗ - Main link. Takes n on the left
Ṛ       - Split n into digits and reverse
Ḍ      - Convert back into an integer; rev(n)
ɗ - Combine the previous three links into a dyad
with n on the right and rev(n) on the left:
æl    -   lcm(n,rev(n))
g  -   gcd(n, rev(n))
:   -   Divide lcm(n,rev(n)) by gcd(n, rev(n))
• I must be missing something here since no one uses that approach, but with a*b/gcd(a,b)=lcm(a,b) this should work for 7 bytes: ṚḌæl:gɗ
– ovs
Aug 17, 2021 at 15:10
• @ovs I can't see any obvious problem with that, and it seems to work for all the test cases, nice! Aug 17, 2021 at 15:29

# Pyt, 11 bytes

ĐĐ₫Đ3Ș*↔Ǥ²/

Try it online!

Code Stack Description
Đ n,n Implicit input; duplicate top of stack
Đ n,n,n Duplicate top of stack
n,n,rev(n) Reverse the digits of the top of the stack
Đ n,n,rev(n),rev(n) Duplicate top of stack
n,rev(n),rev(n),n Flip the order of the top three items on the stack
* n,rev(n),rev(n)*n Multiply the top two items on the stack
rev(n)*n,rev(n),n Flip the stack
Ǥ² rev(n)*n,gcd(n,rev(n))^2 Take the gcd of n and rev(n) and square it
/ rev(n)*n/(gcd(n,rev(n))^2) Divide; implicit print

# Perl 5, 72 bytes

71 bytes of code + 1 flag (-p)

$t=$_*($b=reverse);($_,$b)=(abs$_-$b,$_>$b?$b:$_)while$_-$b;$_=$t/$_**2

Try it online!

# Pyke, 8 bytes

_]FbP).^B

Try it here!

Takes input as a string.

_         -    reversed(input)
]        -   [^, input]
FbP)    -  for i in ^:
b      -     int(^)
P     -    factors(^)
.^  -  xor_seq(^)
B - product(^)

# Python 2, 70 bytes

Thanks to i cri everytim.

def f(n):g=int(n[::-1]);print n*g/gcd(n,g)**2
from fractions import*

Try it online!

# Python 2, 77 bytes

Note that in Python 2, you cannot use the math.gcd() method, and you must do it "by hand".

y=lambda a,b:b and y(b,a%b)or a
def f(n):g=int(n`[::-1]);print n*g/y(n,g)**2

Try it online!

• Python 3 has gcd as fractions.gcd. Sep 8, 2017 at 17:52
• @icrieverytim That's why I chose to solve it in Python 2. Sep 8, 2017 at 18:00
• ...Whoops, I meant Python 2. Python 3 has math.gcd. Sep 8, 2017 at 18:15
• @icrieverytim done. Sep 8, 2017 at 18:17

# Ohm, 9 bytes

DR«D]Æ┴/µ

Try it online!

• psst... Ohm v2 is out and better than ever! Sep 10, 2017 at 0:35

# Java 8, 158150148138125123116 107 + 19 bytes

i->{int o,r,f,t=f=i;i=r=i.valueOf(""+new StringBuffer(t+"").reverse());while(t>0)t=i%(i=t);return f/i*r/i;}

Try it online!

• In the while loop, you can replace t!=0 by t>0, since t will never be negative. f*r/(i*i) is the same as f/i*r/i. You can drop f=t; and r=i; if you chain the assignment of i and t.
– Luke
Sep 9, 2017 at 7:57
• The while loop can be written as while(t>0)t=i%(i=t); (-11 bytes). Sep 11, 2017 at 11:24

# Vyxal, 7 bytes

Ṙ∆Ŀ?Ḃġ/

Try it Online!

Ṙ       # Reverse
∆Ŀ     # LCM with that
?    # Take input
Ḃ   # Dupe-reverse
ġ  # GCD
/ # Quoitent

Would be 5 bytes if parallel apply worked with digraphs.

# J-uby, 25 bytes

Uses ovs' technique.

:/%[:lcm%r=S|:~|Z,:gcd%r]

Attempt This Online!

## Explanation

:/ % [:lcm % r = S | :~ | Z, :gcd % r]

r = S | :~ | Z             # Helper function: Convert to string, reverse, convert back to int
:/ % [                     ,         ]  # Divide...
:lcm % r                          #   LCM of input and its reverse by
:gcd % r   #   GCD of input and its reverse

## J-uby, 32 bytes

We can get rid of the awkward helper function r but at a cost of seven bytes:

:/%(-[:%&:lcm,:%&:gcd]^(S|:~|Z))

Attempt This Online!