罕见因子数


15

基于聊天消息

挑战

给定输入数字n > 9,构造其反向数,而忽略前导零。然后,构建一个所有数量均不相同的主要因素的列表。将这些因子相乘以创建输入的非常见因子编号

或者换一种说法:如果rev(n)表示整数的十进制反转n,请计算的乘积nrev(n)除以gcd(n, rev(n))

输出该号码。

工作的例子

例如,2244反转为4422。第一个[2, 2, 3, 11, 17]的素数因子为,反之的素数因子为[2, 3, 11, 67]。不常见的乘数是[2, 17, 67]2278输出也是。

再举一个例子,1234反转为4321。乘积为5332114,GCD为1,因此输出为5332114

进一步说明

显然,回文数的所有因素与其相反的因素都相同,因此在这种情况下,输出为1n*n/n^2)。显然,输出也可能是所有因素的乘积(例如,gcd为1 -输入及其反向为互质),如1234示例所示。

规则

  • 可以假定输入和输出适合您语言的本机整数类型。
  • 输入和输出可以任何方便的格式给出。
  • 完整的程序或功能都是可以接受的。如果是函数,则可以返回输出而不是打印输出。
  • 如果可能,请提供一个在线测试环境的链接,以便其他人可以尝试您的代码!
  • 禁止出现标准漏洞
  • 这是因此所有常见的高​​尔夫规则都适用,并且最短的代码(以字节为单位)获胜。

例子

in
out

17
1207

208
41704

315
1995

23876
101222302

我们可以假设输入中没有前导零吗?
Xcoder先生17年

1
@ Mr.Xcoder嗯?您的意思是尾随零?
Erik the Outgolfer

@EriktheOutgolfer不,前导零正是我的意思。另外
Xcoder先生,17年

3
第二个测试用例应该是1995(我相信)
Xcoder先生17年

1
@LuisMendo谢谢。很好的补充。
AdmBorkBork

Answers:


6

05AB1E,6个字节

‚D¿÷P

使用05AB1E编码。在线尝试!

说明

‚        # 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

挑战中提供的公式的一个不错,简单的替代方法-+1。在Japt中进行了相同的尝试,但结果比我已有的多了2个字节。
毛茸茸的

5

J,18个字节

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

在线尝试!

或者(在第二个方法中使用@Adnan的方法),

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

J,15个字节(@miles的解决方案)

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

说明

这只是OP给出的算法的直接实现。

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

说明,@ miles的解决方案

非常聪明。

*/@(,%+.)|.&.":
         |.&.":  Reverse digits
           &.":   Convert to string, apply next function, and undo conversion
         |.       Reverse
   (,%+.)        Divide n and reverse(n) by GCD of both
*/               Product

2
15字节*/@(,%+.)|.&.":
英里

@miles,我喜欢秘密技巧
cole

@miles真的很滑。
乔纳

为什么不提交15字节版本作为您的主要解决方案?
毛茸茸的

@Shaggy不确定。我倾向于用“它与我自己的显着不同”来回应,但实际上只是两个优化。稍后再更新。
科尔



2

JavaScript(ES7),67 64字节

这么多字节只是为了反转数字:(

将输入作为字符串。

n=>n*(x=[...n].reverse().join``)/(g=(y,z)=>z?g(z,y%z):y)(n,x)**2

尝试一下

o.innerText=(f=
n=>n*(x=[...n].reverse().join``)/(g=(y,z)=>z?g(z,y%z):y)(n,x)**2
)(i.value="10");oninput=_=>o.innerText=f(i.value)
<input id=i min=10 type=number><pre id=o>



2

R108 89字节

-19个字节,感谢plannapus的gcd算法

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}

这将尝试分配至少一个大小为4*n字节的向量(我认为多达4个),因此这将为足够大的空间引发内存错误n

在线尝试!



1

MATL13 12 11字节

tVPU*1MZdU/

在线尝试!验证所有测试用例

说明

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



1

Japt13 12 11字节


sw
*V/yU ²

尝试一下


说明

整数的隐式输入U。开头的空行可防止以下行被覆盖U

sw

转换U为字符串(s),将其反转(w),转换回整数并分配给variable V

*V

UV

/

划分。

yU

V和的GCD U

²

平方。结果整数的隐式输出。


备用,13个字节

只因为我喜欢能够使用N

NpUsw)mxNry)×

尝试一下


GCD的聪明技巧。我认为该算法实际上可能比当前的Jelly解决方案更短...
ETHproductions's

@ETHproductions在Jelly中,GCD最终会变长...
Erik the Outgolfer

@EriktheOutgolfer我“拥有”一个8字节的版本,但这涉及将两个dyad的结果相除,我不确定如何正确地做到这一点……
ETHproductions 2017年


1

x86机器代码,39个字节

;;; 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

上面的函数计算指定输入参数的“不常见因数”。遵循基于寄存器的__fastcall调用约定,该参数在ECX寄存器中传递。EAX与所有x86调用约定一样,结果返回到寄存器中。

在线尝试!

以这种紧凑的形式花费了很长时间,但这是一个有趣的练习。扭曲的地段,以获得最优化的寄存器调度可能,在x86的约束下DIV指令的隐含操作数,并尝试使用的短编码MUL,并XCHG尽可能说明。我很想知道是否有人可以想到另一种进一步缩短它的方法。到最后我的脑袋已经炸透了。谢谢下次看到编译器!(虽然这方法比编译器会产生更好的代码...特别是如果你稍微调整了它没有大小限制,删除之类的东西XCHG。)




0

Python 2,70个字节

多亏了我给每个人添了光

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

在线尝试!

Python 2,77个字节

请注意,在Python 2中,您不能使用该math.gcd()方法,而必须“手动”执行。

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

在线尝试!


Python 3具有gcdas fractions.gcd
–totalhuman

@icrieverytim这就是为什么我选择来解决它在Python 2
Xcoder先生

...糟糕,我的意思是Python2。Python3具有math.gcd
–totalhuman

@icrieverytim完成。
Xcoder先生17年


0

Java 8,158 150 148 138 125 123 116 107 + 19字节

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;}

在线尝试!


1
在while循环,可以取代t!=0通过t>0,因为T将永远是负面的。f*r/(i*i)与相同f/i*r/i。您可以删除f=t;r=i;如果您链的分配it
路加福音

1
while循环可以写为while(t>0)t=i%(i=t);(-11字节)。
Nevay
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.