Product of Divisors

21

1

Challenge

Given a positive integer, return the product of its divisors, including itself.

This is sequence A007955 in the OEIS.

Test Cases

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

Scoring

This is , so the shortest answer in each language wins!

musicman523

Posted 2017-07-08T05:49:54.503

Reputation: 4 472

2Interesting note (though probably not that useful for this challenge): the product of all divisors of n is always n^((number of divisors of n)/2). – Wojowu – 2017-07-09T07:01:53.540

Answers

13

05AB1E, 2 bytes

ÑP

Try it online!

Explanation

Ñ    # divisors
 P   # product

Emigna

Posted 2017-07-08T05:49:54.503

Reputation: 50 798

By first look I'd say this solution belongs under P, but something holds me off.. – Uriel – 2017-09-18T11:55:48.497

7

Japt, 3 bytes

â ×

Try it online!

Explanation

â ×  // implicit integer input

â    // get integer divisors
  ×  // get product of array

Justin Mariner

Posted 2017-07-08T05:49:54.503

Reputation: 4 746

Damn it, how did you ninja me?! :p Will delete mine when I get to a computer (whenever that might be). – Shaggy – 2017-07-08T06:33:29.340

@Shaggy I'm surprised, since I just found out about both â and × when writing this answer – Justin Mariner – 2017-07-08T06:38:17.477

I was slowed down by the min. character limit! – Shaggy – 2017-07-09T22:32:11.960

5

Jelly, 3 bytes

ÆDP

Try it online!

Leaky Nun

Posted 2017-07-08T05:49:54.503

Reputation: 45 011

5

Python 3, 42 41 bytes

Saved 1 byte thanks to Leaky Nun!

f=lambda i,k=1:k>i or k**(i%k<1)*f(i,k+1)

Try it online!

musicman523

Posted 2017-07-08T05:49:54.503

Reputation: 4 472

1(1,k)[i%k<1] is equivalent to k**(i%k<1) – Leaky Nun – 2017-07-08T06:14:04.863

Wow that's awesome, thank you! – musicman523 – 2017-07-08T06:16:53.653

5

MATL, 3 bytes

Z\p

Try it online!

James

Posted 2017-07-08T05:49:54.503

Reputation: 54 537

4

Haskell, 35 34 bytes

-1 thanks to ovs

f n=product[x|x<-[2..n],n`mod`x<1]

Try it online!

bartavelle

Posted 2017-07-08T05:49:54.503

Reputation: 1 261

3

Pyth, 6 bytes

*Fs{yP

Test suite.

Leaky Nun

Posted 2017-07-08T05:49:54.503

Reputation: 45 011

3

Alice, 12 bytes

/o
\i@/Bdt&*

Try it online!

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.

Martin Ender

Posted 2017-07-08T05:49:54.503

Reputation: 184 808

3

Okx

Posted 2017-07-08T05:49:54.503

Reputation: 15 025

3Me scrolling through answers: plain monospaced code, plain monospaced code, plain... bold, serif code? :-P – ETHproductions – 2017-07-08T11:31:09.493

@ETHproductions Hehe. – Okx – 2017-07-08T11:32:11.420

4@ETHproductions I actually coded this answer on iOS which means I can't actually see the characters. – Okx – 2017-07-08T11:35:01.193

That's... quite impressive. – ETHproductions – 2017-07-08T11:44:45.020

@ETHproductions remember ESMin? – Mama Fun Roll – 2017-07-09T02:39:44.697

2@MamaFunRoll Now that is a name I have not heard in a long, long time... ;-) – ETHproductions – 2017-07-09T03:33:50.193

@ETHproductions Well, at least Neim has a codepage...and ESMin is essentially superseded by Japt... – Erik the Outgolfer – 2017-07-09T08:28:17.110

@EriktheOutgolfer ESMin is/was a completely different beast from Japt... it had a codepage, but the codepage was had 1024 chars :P – Mama Fun Roll – 2017-07-10T16:29:59.747

@MamaFunRoll Well, they're both essentially JavaScript, just ESMin is much longer and uses Unicode. ;) – Erik the Outgolfer – 2017-07-10T18:02:54.920

3

R, 28 bytes

v=scan():1;prod(v[!v[1]%%v])

Try it online!

Scarabee

Posted 2017-07-08T05:49:54.503

Reputation: 131

2

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

pizzapants184

Posted 2017-07-08T05:49:54.503

Reputation: 3 174

2You didn't actually include the bytecount. – Erik the Outgolfer – 2017-07-08T14:05:36.270

@EriktheOutgolfer Whoops! Fixed. – pizzapants184 – 2017-07-08T21:36:13.600

2

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.

Try it online!

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

pizzapants184

Posted 2017-07-08T05:49:54.503

Reputation: 3 174

You can save 4 bytes by (1) counting backwards, (2) removing the parentheses around the p*= expression, and (3) putting a statement in the body of the for loop to drop a comma. I also like to use global vars, rather than adding extra parameters. This avoids undefined behavior, without costing any bytes. Final version: p,a;f(x){for(p=1,a=x;a;--a)p*=x%a?1:a;return p;} – Cody Gray – 2017-07-08T15:13:15.053

You can replace return p; with p=p; and save five bytes. – Jonathan Frech – 2017-09-19T06:08:36.287

To save another byte, you can replace p,a;f(x) with f(x,p,a). – Jonathan Frech – 2017-09-19T06:09:33.703

If you use local instead of global variables, you can even get rid of the entire return p; and save not five, but nine bytes. (TIO)

– Jonathan Frech – 2017-09-19T06:14:40.243

2

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

Shaggy

Posted 2017-07-08T05:49:54.503

Reputation: 24 623

1Why not just the ES6-compatible (n%i?1:i)? (This wouldn't save any byte, though.) – Arnauld – 2017-07-08T09:55:40.003

@Arnauld: because half 6 is clearly too early in the morning for phone golf! :D I had the ternary reversed when I spotted Leaky's tip! – Shaggy – 2017-07-08T11:01:23.977

2

TI-Basic, 24 14 13 bytes

Saved 1 byte thanks to lirtosiast

:√(Ans^sum(not(fPart(Ans/randIntNoRep(1,Ans

Oki

Posted 2017-07-08T05:49:54.503

Reputation: 311

1Do you need the int(? – lirtosiast – 2017-07-11T01:12:02.070

2

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!

Cody Gray

Posted 2017-07-08T05:49:54.503

Reputation: 2 639

1

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

steenbergh

Posted 2017-07-08T05:49:54.503

Reputation: 7 772

1

Pari/GP, 18 bytes

n->n^(numdiv(n)/2)

Try it online!

alephalpha

Posted 2017-07-08T05:49:54.503

Reputation: 23 988

1

PHP, 45 bytes

for($p=1;$d++<$argn;)$argn%$d?:$p*=$d;echo$p;

Try it online!

Jörg Hülsermann

Posted 2017-07-08T05:49:54.503

Reputation: 13 026

1

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@#&

J42161217

Posted 2017-07-08T05:49:54.503

Reputation: 15 931

1

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

Copper

Posted 2017-07-08T05:49:54.503

Reputation: 3 684

1

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.

Try it online!

Mario Ishac

Posted 2017-07-08T05:49:54.503

Reputation: 491

1

Java (OpenJDK 8), 52 51 bytes

n->{int r=n,d=0;for(;++d<n;)r*=n%d<1?d:1;return r;}

Try it online!

Thanks LeakyNun for saving 1 byte!

Olivier Grégoire

Posted 2017-07-08T05:49:54.503

Reputation: 10 647

1n->{int r=n,d=0;for(;++d<n;)r*=n%d<1?d:1;return r;} – Leaky Nun – 2017-07-10T06:37:04.030

1

MY, 4 bytes

Hex:

1A 3A 54 27

Explanation:

1A - Input as an integer
3A - Factors
54 - Product
27 - Output (with newline)

Zacharý

Posted 2017-07-08T05:49:54.503

Reputation: 5 710

1

RProgN 2, 2 bytes

ƒ*

Another language with built ins for divisors and product.

Try it online!

ATaco

Posted 2017-07-08T05:49:54.503

Reputation: 7 898

0

Perl 6, 22 bytes

{[*] grep $_%%*,1..$_}

Try it online!

Sean

Posted 2017-07-08T05:49:54.503

Reputation: 4 136

0

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

Try it online!

officialaimm

Posted 2017-07-08T05:49:54.503

Reputation: 2 739

0

J, 19 bytes

*/}.I.(=<.)(%i.@>:)

Explanation coming later...

Try it online!

Jonah

Posted 2017-07-08T05:49:54.503

Reputation: 8 729

0

Octave, 27 bytes

@(n)prod(find(~mod(n,1:n)))

This defines an anonymous function.

Try it online!

Luis Mendo

Posted 2017-07-08T05:49:54.503

Reputation: 87 464

0

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

Try it online!

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

Steadybox

Posted 2017-07-08T05:49:54.503

Reputation: 15 798

0

Axiom, 23 bytes

h(x)==x^(#divisors x/2)

This is a translation in Axiom of alephalpha solution

RosLuP

Posted 2017-07-08T05:49:54.503

Reputation: 3 036

0

Ruby, 36 bytes

->r{eval (1..r).select{|c|r%c<1}*?*}

Try it online!

G B

Posted 2017-07-08T05:49:54.503

Reputation: 11 099

0

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

Taylor Scott

Posted 2017-07-08T05:49:54.503

Reputation: 6 709