| Bytes | Lang | Time | Link |
|---|---|---|---|
| 032 | AWK | 250905T152824Z | xrs |
| 002 | Thunno 2 | 230715T100911Z | The Thon |
| 002 | Pyt | 230115T205320Z | Kip the |
| nan | 220927T032112Z | bigyihsu | |
| 060 | Prolog SWI | 220927T025211Z | naffetS |
| 002 | Vyxal | 220926T231237Z | aketon |
| nan | Fig | 220926T181149Z | Seggan |
| 007 | APL Dyalog Unicode | 201220T203601Z | Kamila S |
| 002 | Husk | 201126T040255Z | Razetime |
| 031 | Perl 5 ap | 201126T012642Z | Xcali |
| 023 | x8664 machine code | 201125T165836Z | anatolyg |
| 024 | SageMath | 201125T110157Z | Robin Ho |
| 003 | MathGolf | 201125T102137Z | Kevin Cr |
| 040 | PowerShell Core | 201125T045537Z | Julian |
| 027 | Julia | 201125T090619Z | MarcMush |
| 003 | Arn | 201125T024727Z | ZippyMag |
| 043 | Factor | 201125T024117Z | Bubbler |
| 002 | RProgN 2 | 170918T001407Z | ATaco |
| 048 | Excel VBA | 170917T195123Z | Taylor R |
| 013 | TIBasic | 170708T113411Z | Oki |
| 036 | Ruby | 170710T093525Z | G B |
| 032 | JavaScript ES7 | 170708T065706Z | Shaggy |
| 051 | Java OpenJDK 8 | 170709T085149Z | Olivier |
| 004 | MY | 170709T143201Z | Adalynn |
| 045 | Python 3 | 170709T020811Z | Mario Is |
| 353 | Shakespeare Programming Language | 170708T230222Z | Copper |
| 048 | C gcc | 170708T063839Z | pizzapan |
| 024 | TIBasic TI84 Plus CE | 170708T060512Z | pizzapan |
| 023 | Axiom | 170708T162708Z | user5898 |
| 026 | x8664 Machine Code | 170708T154255Z | Cody Gra |
| 017 | Mathematica | 170708T132156Z | ZaMoC |
| 088 | Fortran 95 | 170708T121210Z | Steadybo |
| 028 | R | 170708T120643Z | Scarabee |
| 045 | PHP | 170708T111948Z | Jör |
| 002 | Neim | 170708T111447Z | Okx |
| 050 | Python 2 | 170708T072824Z | 0xffcour |
| 027 | Octave | 170708T100859Z | Luis Men |
| 018 | Pari/GP | 170708T080209Z | alephalp |
| 019 | J | 170708T073919Z | Jonah |
| 012 | Alice | 170708T064454Z | Martin E |
| 022 | Perl 6 | 170708T064057Z | Sean |
| 034 | Haskell | 170708T062403Z | bartavel |
| 022 | QBIC | 170708T062430Z | steenber |
| 002 | 05AB1E | 170708T062621Z | Emigna |
| 003 | Japt | 170708T061739Z | Justin M |
| 041 | Python 3 | 170708T060625Z | musicman |
| 003 | MATL | 170708T061009Z | DJMcMayh |
| 006 | Pyth | 170708T060106Z | Leaky Nu |
| 003 | Jelly | 170708T055915Z | Leaky Nu |
Go, 65 64 bytes
func p(n int)int{p,i:=1,1
for;i<=n;i++{if n%i<1{p*=i}}
return p}
Ungolfed
- -1 swapping
==0for<1(@Steffan)
func product(s []int) int {
p := 1
for _,e := range s {
p *= e
}
return p
}
func divisors(n int) (d []int) {
for i := 1; i <= n; i++ {
if n % i == 0 {
d = append(d, i)
}
}
return d
}
x86-64 machine code, 23 bytes
Hexdump:
44 8b c1 6a 01 41 8b c0 99 f7 f9 85 d2 75 04 5a
f7 ea 50 e2 f0 58 c3
Uses the Microsoft calling convention - input in ecx, output in eax. It only supports 32-bit intermediate and final results, to make the code shorter.
Assembly code (can be assembled by ml64):
.CODE
my PROC
mov r8d, ecx; // r8 = input number
push 1; // number on stack = product
myloop:
mov eax, r8d; // prepare divisor
cdq; // in edx:eax
idiv ecx; // divide
test edx, edx; // check remainder
jnz myskip; // nonzero? - not a divisor
pop rdx; // get product from stack
imul edx; // multiply
push rax; // save product on stack
myskip:
loop myloop; // check next number, down to 0
pop rax; // restore stack to proper position
ret; // return
my ENDP
END
I used x64 only for its calling convention - so I would not have to save and restore a general-purpose register - x64 calling conventions permit clobbering r8 and r9.
I used a variant of imul with implicit input and output in eax - it has a shorter encoding (2 bytes), and doesn't require mov to return the result from the function at the end. So it actually multiplies the quotients, not the divisors; the final result remains correct.
Disassembly (to see instruction sizes):
00007FF7F91E1870 44 8B C1 mov r8d,ecx
00007FF7F91E1873 6A 01 push 1
myloop:
00007FF7F91E1875 41 8B C0 mov eax,r8d
00007FF7F91E1878 99 cdq
00007FF7F91E1879 F7 F9 idiv eax,ecx
00007FF7F91E187B 85 D2 test edx,edx
00007FF7F91E187D 75 04 jne myloop+0Eh (07FF7F91E1883h)
00007FF7F91E187F 5A pop rdx
00007FF7F91E1880 F7 EA imul edx
00007FF7F91E1882 50 push rax
myskip:
00007FF7F91E1883 E2 F0 loop myloop (07FF7F91E1875h)
00007FF7F91E1885 58 pop rax
00007FF7F91E1886 C3 ret
SageMath, 24 bytes
f=compose(prod,divisors)
Straightforward and direct. Is there a shorter way to do it in Sage?
MathGolf, 3 bytes
─ε*
Explanation:
─ # Get a list of divisors of the (implicit) input-integer
ε # Reduce this list by:
* # Subtracting
# (after which the entire stack joined together is output implicitly as result)
Arn, 3 bytes
Þ┤â
Explained
Unpacked: *\*.
*\ fold with multiplication
*. a list of factors of the input
Factor, 43 bytes
[ dup [1,b] [ dupd divisor? ] count 2 / ^ ]
Uses the formula n^((number of divisors of n)/2).
Usually mod 0 = (7 bytes) is shorter than divisor? (8 bytes), but the latter is used here to auto-load math.functions to disambiguate ^ (which happens to appear in a regex lib). Auto-use is weird. The unambiguous alternative to ^ exists (fpow), but it's longer.
[ ! anonymous lambda
dup [1,b] ! ( n {1..n} )
[ ... ] count ! count the elements that satisfy the predicate...
dupd ! ( n i -- n n i )
divisor? ! ( n ? ) tests if i is a divisor of n
2 / ^ ! halve the divisor count and raise n to the power
]
Factor, 45 bytes
USE: math.primes.factors
[ divisors product ]
Uses divisors built-in, but obviously the library import is expensive.
Excel VBA, 48 Bytes
Anonymous VBE immediate window function that takes input from range [A1] and outputs to the VBE immediate window
p=1:For i=1To[A1]:p=p*IIf([A1]mod i,1,i):Next:?p
TI-Basic, 24 14 13 bytes
Saved 1 byte thanks to lirtosiast
:√(Ans^sum(not(fPart(Ans/randIntNoRep(1,Ans
JavaScript (ES7), 32 bytes
n=>g=(i=n)=>i?i**!(n%i)*g(i-1):1
Saved a couple of bytes by borrowing Leaky's tip on musicman's Python solution.
Try it
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>
Alternative (ES6), 32 bytes
n=>g=(i=n)=>i?(n%i?1:i)*g(i-1):1
Java (OpenJDK 8), 52 51 bytes
n->{int r=n,d=0;for(;++d<n;)r*=n%d<1?d:1;return r;}
Thanks LeakyNun for saving 1 byte!
MY, 4 bytes
Hex:
1A 3A 54 27
Explanation:
1A - Input as an integer
3A - Factors
54 - Product
27 - Output (with newline)
Python 3, 45 bytes
lambda _:_**(sum(_%-~i<1for i in range(_))/2)
Let x be a number. Both y and z will be divisors of x if y * z = x. Therefore, y = x / z. Let's say a number d has 6 divisiors, due to this observation the divisors will be a, b, c, d / a, d / b, d / b. If we multiply all these numbers (the point of the puzzle), we obtain d * d * d = d ^ 3. In general, for e with a number of f divisors, the product of said divisors will be e ^ (f / 2), which is what the lambda does.
Shakespeare Programming Language, 353 bytes
.
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]
Ungolfed version:
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]
I'm using this SPL compiler to run the program.
Run with:
$ python splc.py product-of-divisors.spl > product-of-divisors.c
$ gcc product-of-divisors.c -o pod.exe
$ echo 30 | ./pod
810000
C (gcc), 52 48 bytes
p,a;f(x){for(p=1,a=x;a;a--)p*=x%a?1:a;return p;}
-4 bytes thanks to Cody Gray
A function that takes in an integer and returns the product of it's divisors.
Ungolfed:
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;
}
TI-Basic (TI-84 Plus CE), 24 bytes
Prompt X
1
For(A,1,X
If not(remainder(X,A
AAns
End
Full program: prompts user for input; returns output in Ans, a special variable that (basically) stores the value of the latest value calculated.
Explanation:
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
Axiom, 23 bytes
h(x)==x^(#divisors x/2)
This is a translation in Axiom of alephalpha solution
x86-64 Machine Code, 26 bytes
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
The above code defines a function that takes a single parameter (the input value, a positive integer) in EDI (following the System V AMD64 calling convention used on Gnu/Unix), and returns a single result (the product of divisors) in EAX.
Internally, it computes the product of divisors using an (extremely inefficient) iterative algorithm, similar to pizzapants184's C submission. Basically, it uses a counter to loop through all of the values between 1 and the input value, checking to see if the current counter value is a divisor of the input. If so, it multiplies that into the running total product.
Ungolfed assembly language mnemonics:
; 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
The fact that the IDIV instruction uses hard-coded operands for the dividend cramps my style a bit, but I think this is pretty good for a language that has no built-ins but basic arithmetic and conditional branches!
Mathematica, 17 bytes
for those who can't view deleted answers (DavidC's answer), this is the code in Mathematica with the help of @MartinEnder
1##&@@Divisors@#&
Fortran 95, 88 bytes
function l(k)
n=0
l=1
do while(n<k)
n=n+1
if(MODULO(k,n)==0)then
l=l*n
end if
end do
end
Ungolfed:
integer function l(k)
implicit none
integer :: n, k
n=0
l=1
do while (n<k)
n=n+1
if (MODULO(k,n) == 0) then
l=l*n
end if
end do
end function l
Python 2, 52 50 bytes
- Thanks @ovs for 2 bytes:
m*=n%i>0 or i
i=n=input()
m=1
while i:m*=n%i>0 or i;i-=1
print m
Alice, 12 bytes
/o
\i@/Bdt&*
Explanation
This is just the regular framework for decimal I/O:
/o
\i@/...
Then the program is:
B Get all divisors of the input.
dt Get the stack depth minus 1.
&* Multiply the top two stack elements that many times, folding multiplication
over the stack.
QBIC, 22 bytes
[:|~b/a=b'\`a|q=q*a}?q
Explanation
[:| FOR a = 1; a <= input (b); a++
b/a=b'\`a 'a' is a proper divisor if integer division == float division
~ | IF that's true
q=q*a THEN multiply running total q (starts as 1) by that divsor
} NEXT
?q Print q
Japt, 3 bytes
â ×
Explanation
â × // implicit integer input
â // get integer divisors
× // get product of array