挑战
给定一个正整数,返回其除数(包括自身)的乘积。
测试用例
1:1 2:2 3:3 4:8 5:5 6:36 7:7 8:64 9:27 10:100 12:1728 14:196 24:331776 25:125 28:21952 30:810000
计分
这是代码高尔夫球,因此每种语言中最短的答案将获胜!
给定一个正整数,返回其除数(包括自身)的乘积。
1:1 2:2 3:3 4:8 5:5 6:36 7:7 8:64 9:27 10:100 12:1728 14:196 24:331776 25:125 28:21952 30:810000
这是代码高尔夫球,因此每种语言中最短的答案将获胜!
Answers:
31 C9 8D 71 01 89 F8 FF C1 99 F7 F9 85 D2 75 03 0F AF F1 39 F9 7C EE 89 F0 C3
上面的代码定义了一个函数,该函数采用EDI
(遵循Gnu / Unix上使用的System V AMD64调用约定)中的单个参数(输入值,正整数),并在中返回单个结果(除数的乘积)EAX
。
在内部,它使用(极其无效的)迭代算法来计算除数的乘积,类似于pizzapant184的C提交。基本上,它使用计数器在1和输入值之间的所有值之间循环,以检查当前计数器值是否为输入的除数。如果是这样,它将乘以该乘积。
非公开的汇编语言助记符:
; Parameter is passed in EDI (a positive integer)
ComputeProductOfDivisors:
xor ecx, ecx ; ECX <= 0 (our counter)
lea esi, [rcx + 1] ; ESI <= 1 (our running total)
.CheckCounter:
mov eax, edi ; put input value (parameter) in EAX
inc ecx ; increment counter
cdq ; sign-extend EAX to EDX:EAX
idiv ecx ; divide EDX:EAX by ECX
test edx, edx ; check the remainder to see if divided evenly
jnz .SkipThisOne ; if remainder!=0, skip the next instruction
imul esi, ecx ; if remainder==0, multiply running total by counter
.SkipThisOne:
cmp ecx, edi ; are we done yet? compare counter to input value
jl .CheckCounter ; if counter hasn't yet reached input value, keep looping
mov eax, esi ; put our running total in EAX so it gets returned
ret
IDIV
指令使用硬编码的操作数进行除数的事实使我的风格有点拥挤,但是我认为这对于没有内置语言但具有基本算术和条件分支的语言来说非常不错!
Prompt X
1
For(A,1,X
If not(remainder(X,A
AAns
End
完整程序:提示用户输入;返回中的输出Ans
,该特殊变量(基本上)存储计算出的最新值。
说明:
Prompt X # 3 bytes, Prompt user for input, store in X
1 # 2 bytes, store 1 in Ans for use later
For(A,1,X # 7 bytes, for each value of A from 1 to X
If not(remainder(X,A # 8 bytes, If X is divisible by A...
AAns # 3 bytes, ...store (A * Ans) in Ans
End # 1 byte, end For( loop
p,a;f(x){for(p=1,a=x;a;a--)p*=x%a?1:a;return p;}
-4字节感谢Cody Gray
一个接受整数并返回其除数的乘积的函数。
取消高尔夫:
int proddiv(int input) {
int total = 1, loopvar;
for(loopvar = input; loopvar > 0; --loopvar) {
// for loopvar from input down to 1...
total *= (input % loopvar) ? 1 : loopvar;
// ...If the loopvar is a divisor of the input, multiply the total by loopvar;
}
return total;
}
p*=
,以及(3)在for
循环体中放置一条语句以删除逗号。我还喜欢使用全局变量,而不是添加额外的参数。这样可以避免未定义的行为,而不会花费任何字节。最终版本:p,a;f(x){for(p=1,a=x;a;--a)p*=x%a?1:a;return p;}
return p;
为p=p;
并保存五个字节。
p,a;f(x)
为f(x,p,a)
。
n=>g=(i=n)=>i?i**!(n%i)*g(i-1):1
借用Leaky关于音乐人的Python解决方案的技巧,节省了几个字节。
o.innerText=(f=
n=>g=(i=n)=>i?i**!(n%i)*g(i-1):1
)(i.value=1)();oninput=_=>o.innerText=f(+i.value)()
<input id=i type=number><pre id=o>
n=>g=(i=n)=>i?(n%i?1:i)*g(i-1):1
(n%i?1:i)
?(不过,这不会节省任何字节。)
感谢lirtosiast,节省了1个字节
:√(Ans^sum(not(fPart(Ans/randIntNoRep(1,Ans
int(
吗?
.
Ajax,.
Puck,.
Page,.
Act I:.
Scene I:.
[Enter Ajax and Puck]
Ajax:
You cat
Puck:
Listen to thy heart
[Exit Ajax]
[Enter Page]
Scene II:.
Puck:
You sum you cat
Page:
Is Ajax nicer I?If so, is remainder of the quotient Ajax I nicer zero?If not, you product you I.Is Ajax nicer I?If so, let us return to scene II
Scene III:.
Page:
Open thy heart
[Exeunt]
非高尔夫版本:
The Tragedy of the Product of a Moor's Factors in Venice.
Othello, a numerical man.
Desdemona, a product of his imagination.
Brabantio, a senator, possibly in charge of one Othello's factories.
Act I: In which tragedy occurs.
Scene I: Wherein Othello and Desdemona have an enlightened discussion.
[Enter Othello and Desdemona]
Othello:
Thou art an angel!
Desdemona:
Listen to thy heart.
[Exit Othello]
[Enter Brabantio]
Scene II: Wherein Brabantio expresses his internal monologue to Desdemona.
Desdemona:
Thou art the sum of thyself and the wind!
Brabantio:
Is Othello jollier than me?
If so, is the remainder of the quotient of Othello and I better than nothing?
If not, thou art the product of thyself and me.
IS Othello jollier than me?
If so, let us return to scene II!
Scene III: An Epilogue.
Brabantio:
Open thy heart!
[Exeunt]
我正在使用此SPL编译器来运行程序。
运行:
$ python splc.py product-of-divisors.spl > product-of-divisors.c
$ gcc product-of-divisors.c -o pod.exe
$ echo 30 | ./pod
810000
lambda _:_**(sum(_%-~i<1for i in range(_))/2)
设x
一个数字。双方y
并z
会的除数x
如果y * z = x
。因此,y = x / z
。比方说,一些d
有6个divisiors,由于这种观察除数会a
,b
,c
,d / a
,d / b
,d / b
。如果将所有这些数字相乘(谜题的重点),我们将获得d * d * d = d ^ 3
。通常,对于e
有多个f
除数的情况,所述除数的乘积将为e ^ (f / 2)
,这就是lambda所做的。
n->{int r=n,d=0;for(;++d<n;)r*=n%d<1?d:1;return r;}