Product of divisors: Difference between revisions

Content added Content deleted
(J)
m (syntax highlighting fixup automation)
Line 11: Line 11:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F product_of_divisors(n)
<syntaxhighlight lang="11l">F product_of_divisors(n)
V ans = 1
V ans = 1
V i = 1
V i = 1
Line 24: Line 24:
R ans
R ans


print((1..50).map(n -> product_of_divisors(n)))</lang>
print((1..50).map(n -> product_of_divisors(n)))</syntaxhighlight>


{{out}}
{{out}}
Line 33: Line 33:
=={{header|Action!}}==
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
{{libheader|Action! Tool Kit}}
<lang Action!>INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit


PROC ProdOfDivisors(INT n REAL POINTER prod)
PROC ProdOfDivisors(INT n REAL POINTER prod)
Line 66: Line 66:
PrintR(prod) Put(32)
PrintR(prod) Put(32)
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Product_of_divisors.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Product_of_divisors.png Screenshot from Atari 8-bit computer]
Line 77: Line 77:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{Trans|Fortran}}
{{Trans|Fortran}}
<lang algol68>BEGIN # product of divisors - transaltion of the Fortran sample #
<syntaxhighlight lang="algol68">BEGIN # product of divisors - transaltion of the Fortran sample #
[ 1 : 50 ]INT divis;
[ 1 : 50 ]INT divis;
FOR i TO UPB divis DO divis[ i ] := 1 OD;
FOR i TO UPB divis DO divis[ i ] := 1 OD;
Line 89: Line 89:
IF i MOD 5 = 0 THEN print( ( newline ) ) FI
IF i MOD 5 = 0 THEN print( ( newline ) ) FI
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 105: Line 105:


{{Trans|C++}}
{{Trans|C++}}
<lang algol68>BEGIN # find the product of the divisors of the first 100 positive integers #
<syntaxhighlight lang="algol68">BEGIN # find the product of the divisors of the first 100 positive integers #
# calculates the number of divisors of v #
# calculates the number of divisors of v #
PROC divisor count = ( INT v )INT:
PROC divisor count = ( INT v )INT:
Line 146: Line 146:
OD
OD
END
END
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 164: Line 164:
=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
{{Trans|Fortran}}
{{Trans|Fortran}}
<lang algolw>begin % product of divisors - transaltion of the Fortran sample %
<syntaxhighlight lang="algolw">begin % product of divisors - transaltion of the Fortran sample %
integer array divis ( 1 :: 50 );
integer array divis ( 1 :: 50 );
for i := 1 until 50 do divis( i ) := 1;
for i := 1 until 50 do divis( i ) := 1;
Line 174: Line 174:
if i rem 5 = 0 then write()
if i rem 5 = 0 then write()
end for_i
end for_i
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 190: Line 190:


{{Trans|C++}}
{{Trans|C++}}
<lang algolw>begin % find the product of the divisors of the first 100 positive integers %
<syntaxhighlight lang="algolw">begin % find the product of the divisors of the first 100 positive integers %
% calculates the number of divisors of v %
% calculates the number of divisors of v %
integer procedure divisor_count( integer value v ) ; begin
integer procedure divisor_count( integer value v ) ; begin
Line 234: Line 234:
end for_n
end for_n
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 251: Line 251:


=={{header|APL}}==
=={{header|APL}}==
<lang APL>divprod ← ×/(⍸0=⍳|⊢)
<syntaxhighlight lang="apl">divprod ← ×/(⍸0=⍳|⊢)
10 5 ⍴ divprod¨ ⍳50</lang>
10 5 ⍴ divprod¨ ⍳50</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 2 3 8 5
<pre> 1 2 3 8 5
Line 267: Line 267:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>loop split.every:5 to [:string] map 1..50 => [product factors &] 'line [
<syntaxhighlight lang="rebol">loop split.every:5 to [:string] map 1..50 => [product factors &] 'line [
print map line 'i -> pad i 10
print map line 'i -> pad i 10
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 285: Line 285:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRODUCT_OF_DIVISORS.AWK
# syntax: GAWK -f PRODUCT_OF_DIVISORS.AWK
# converted from Go
# converted from Go
Line 315: Line 315:
return(ans)
return(ans)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 327: Line 327:


=={{header|BASIC}}==
=={{header|BASIC}}==
<lang basic>10 N = 50
<syntaxhighlight lang="basic">10 N = 50
20 DIM D(N)
20 DIM D(N)
30 FOR I=1 TO N: D(I)=1: NEXT
30 FOR I=1 TO N: D(I)=1: NEXT
Line 335: Line 335:
70 NEXT J
70 NEXT J
80 NEXT I
80 NEXT I
90 FOR I=1 TO N: PRINT D(I),: NEXT</lang>
90 FOR I=1 TO N: PRINT D(I),: NEXT</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 2 3 8 5
<pre> 1 2 3 8 5
Line 350: Line 350:


==={{header|BASIC256}}===
==={{header|BASIC256}}===
<lang BASIC256>for n = 1 to 50
<syntaxhighlight lang="basic256">for n = 1 to 50
p = n
p = n
for i = 2 to n/2
for i = 2 to n/2
Line 358: Line 358:
print p; chr(9);
print p; chr(9);
next n
next n
end</lang>
end</syntaxhighlight>


==={{header|PureBasic}}===
==={{header|PureBasic}}===
<lang PureBasic>OpenConsole()
<syntaxhighlight lang="purebasic">OpenConsole()
For n.i = 1 To 50
For n.i = 1 To 50
p = n
p = n
Line 371: Line 371:
Next n
Next n
Input()
Input()
CloseConsole()</lang>
CloseConsole()</syntaxhighlight>


==={{header|QBasic}}===
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
<lang QBasic>FOR n = 1 TO 50
<syntaxhighlight lang="qbasic">FOR n = 1 TO 50
p = n
p = n
FOR i = 2 TO n / 2
FOR i = 2 TO n / 2
Line 384: Line 384:
PRINT USING "###########"; p;
PRINT USING "###########"; p;
NEXT n
NEXT n
END</lang>
END</syntaxhighlight>


==={{header|True BASIC}}===
==={{header|True BASIC}}===
<lang qbasic>FOR n = 1 TO 50
<syntaxhighlight lang="qbasic">FOR n = 1 TO 50
LET p = n
LET p = n
FOR i = 2 TO n/2
FOR i = 2 TO n/2
Line 395: Line 395:
PRINT p,
PRINT p,
NEXT n
NEXT n
END</lang>
END</syntaxhighlight>


==={{header|Yabasic}}===
==={{header|Yabasic}}===
<lang yabasic>for n = 1 to 50
<syntaxhighlight lang="yabasic">for n = 1 to 50
p = n
p = n
for i = 2 to n/2
for i = 2 to n/2
Line 406: Line 406:
print p using "###########";
print p using "###########";
next n
next n
end</lang>
end</syntaxhighlight>


=={{header|BQN}}==
=={{header|BQN}}==
<lang bqn>(⊢(×´⊢/˜ 0=|˜ )1+↕)¨∘‿5⥊1+↕50</lang>
<syntaxhighlight lang="bqn">(⊢(×´⊢/˜ 0=|˜ )1+↕)¨∘‿5⥊1+↕50</syntaxhighlight>
{{out}}
{{out}}
<pre>┌─
<pre>┌─
Line 425: Line 425:
=={{header|C}}==
=={{header|C}}==
{{trans|C++}}
{{trans|C++}}
<lang c>#include <math.h>
<syntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
#include <stdio.h>


Line 470: Line 470:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Product of divisors for the first 50 positive integers:
<pre>Product of divisors for the first 50 positive integers:
Line 485: Line 485:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <cmath>
<syntaxhighlight lang="cpp">#include <cmath>
#include <iomanip>
#include <iomanip>
#include <iostream>
#include <iostream>
Line 521: Line 521:
std::cout << '\n';
std::cout << '\n';
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 539: Line 539:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
<lang Lisp>
(format t "~{~a ~}~%"
(format t "~{~a ~}~%"
(loop for a from 1 to 100 collect
(loop for a from 1 to 100 collect
Line 545: Line 545:
when (zerop (rem a b)) do (setf z (* z b))
when (zerop (rem a b)) do (setf z (* z b))
finally (return z))))
finally (return z))))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>1 2 3 8 5 36 7 64 27 100 11 1728 13 196 225 1024 17 5832 19 8000 441 484 23 331776 125 676 729 21952 29 810000 31 32768 1089 1156 1225 10077696 37 1444 1521 2560000 41 3111696 43 85184 91125 2116 47 254803968 343 125000 2601 140608 53 8503056 3025 9834496 3249 3364 59 46656000000 61 3844 250047 2097152 4225 18974736 67 314432 4761 24010000 71 139314069504 73 5476 421875 438976 5929 37015056 79 3276800000 59049 6724 83 351298031616 7225 7396 7569 59969536 89 531441000000 8281 778688 8649 8836 9025 782757789696 97 941192 970299 1000000000 </pre>
<pre>1 2 3 8 5 36 7 64 27 100 11 1728 13 196 225 1024 17 5832 19 8000 441 484 23 331776 125 676 729 21952 29 810000 31 32768 1089 1156 1225 10077696 37 1444 1521 2560000 41 3111696 43 85184 91125 2116 47 254803968 343 125000 2601 140608 53 8503056 3025 9834496 3249 3364 59 46656000000 61 3844 250047 2097152 4225 18974736 67 314432 4761 24010000 71 139314069504 73 5476 421875 438976 5929 37015056 79 3276800000 59049 6724 83 351298031616 7225 7396 7569 59969536 89 531441000000 8281 778688 8649 8836 9025 782757789696 97 941192 970299 1000000000 </pre>


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. PRODUCT-OF-DIVISORS.
PROGRAM-ID. PRODUCT-OF-DIVISORS.


Line 592: Line 592:
DISPLAY OUT-LINE,
DISPLAY OUT-LINE,
MOVE SPACES TO OUT-LINE,
MOVE SPACES TO OUT-LINE,
MOVE 1 TO LINE-PTR.</lang>
MOVE 1 TO LINE-PTR.</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 2 3 8 5
<pre> 1 2 3 8 5
Line 606: Line 606:


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


sub divprod(n: uint32): (prod: uint32) is
sub divprod(n: uint32): (prod: uint32) is
Line 631: Line 631:
end if;
end if;
n := n + 1;
n := n + 1;
end loop;</lang>
end loop;</syntaxhighlight>


{{out}}
{{out}}
Line 648: Line 648:
=={{header|D}}==
=={{header|D}}==
{{trans|C++}}
{{trans|C++}}
<lang d>import std.math;
<syntaxhighlight lang="d">import std.math;
import std.stdio;
import std.stdio;


Line 686: Line 686:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Product of divisors for the first 50positive integers:
<pre>Product of divisors for the first 50positive integers:
Line 702: Line 702:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
{{works with|Factor|0.99 2020-08-14}}
<lang factor>USING: grouping io math.primes.factors math.ranges prettyprint
<syntaxhighlight lang="factor">USING: grouping io math.primes.factors math.ranges prettyprint
sequences ;
sequences ;


"Product of divisors for the first 50 positive integers:" print
"Product of divisors for the first 50 positive integers:" print
50 [1,b] [ divisors product ] map 5 group simple-table.</lang>
50 [1,b] [ divisors product ] map 5 group simple-table.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 723: Line 723:


=={{header|Fortran}}==
=={{header|Fortran}}==
<lang fortran> program divprod
<syntaxhighlight lang="fortran"> program divprod
implicit none
implicit none
integer divis(50), i, j
integer divis(50), i, j
Line 734: Line 734:
write (*,'(I10)',advance='no') divis(i)
write (*,'(I10)',advance='no') divis(i)
30 if (i/5 .ne. (i-1)/5) write (*,*)
30 if (i/5 .ne. (i-1)/5) write (*,*)
end program</lang>
end program</syntaxhighlight>


{{out}}
{{out}}
Line 748: Line 748:
2116 47 254803968 343 125000</pre>
2116 47 254803968 343 125000</pre>
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>dim p as ulongint
<syntaxhighlight lang="freebasic">dim p as ulongint
for n as uinteger = 1 to 50
for n as uinteger = 1 to 50
p = n
p = n
Line 756: Line 756:
print p,
print p,
next n
next n
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>1 2 3 8 5 36
<pre>1 2 3 8 5 36
Line 768: Line 768:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 800: Line 800:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 818: Line 818:


=={{header|GW-BASIC}}==
=={{header|GW-BASIC}}==
<lang gwbasic>
<syntaxhighlight lang="gwbasic">
10 FOR N = 1 TO 50
10 FOR N = 1 TO 50
20 P# = N
20 P# = N
Line 825: Line 825:
50 NEXT I
50 NEXT I
60 PRINT P#,
60 PRINT P#,
70 NEXT N</lang>
70 NEXT N</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 835: Line 835:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List.Split (chunksOf)
<syntaxhighlight lang="haskell">import Data.List.Split (chunksOf)


------------------------- DIVISORS -----------------------
------------------------- DIVISORS -----------------------
Line 872: Line 872:


justifyRight :: Int -> Char -> String -> String
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</lang>
justifyRight n c = (drop . length) <*> (replicate n c <>)</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Sums of divisors of [1..100]:
<pre>Sums of divisors of [1..100]:
Line 919: Line 919:


=={{header|J}}==
=={{header|J}}==
<lang J> {{ */ */@>,{ (^ i.@>:)&.>/ __ q: y }}@>:i.5 10x
<syntaxhighlight lang="j"> {{ */ */@>,{ (^ i.@>:)&.>/ __ q: y }}@>:i.5 10x
1 2 3 8 5 36 7 64 27 100
1 2 3 8 5 36 7 64 27 100
11 1728 13 196 225 1024 17 5832 19 8000
11 1728 13 196 225 1024 17 5832 19 8000
441 484 23 331776 125 676 729 21952 29 810000
441 484 23 331776 125 676 729 21952 29 810000
31 32768 1089 1156 1225 10077696 37 1444 1521 2560000
31 32768 1089 1156 1225 10077696 37 1444 1521 2560000
41 3111696 43 85184 91125 2116 47 254803968 343 125000</lang>
41 3111696 43 85184 91125 2116 47 254803968 343 125000</syntaxhighlight>
=={{header|Java}}==
=={{header|Java}}==
{{trans|C++}}
{{trans|C++}}
<lang java>public class ProductOfDivisors {
<syntaxhighlight lang="java">public class ProductOfDivisors {
private static long divisorCount(long n) {
private static long divisorCount(long n) {
long total = 1;
long total = 1;
Line 963: Line 963:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Product of divisors for the first 50 positive integers:
<pre>Product of divisors for the first 50 positive integers:
Line 985: Line 985:
Since a `divisors` function is more likely to be generally useful than a "product of divisors"
Since a `divisors` function is more likely to be generally useful than a "product of divisors"
function, this entry implements the latter in terms of the former, without any appreciable cost because a streaming approach is used.
function, this entry implements the latter in terms of the former, without any appreciable cost because a streaming approach is used.
<lang jq># divisors as an unsorted stream
<syntaxhighlight lang="jq"># divisors as an unsorted stream
def divisors:
def divisors:
if . == 1 then 1
if . == 1 then 1
Line 1,008: Line 1,008:
# For pretty-printing
# For pretty-printing
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
</syntaxhighlight>
</lang>
'''Example'''
'''Example'''
<lang jq>"n product of divisors",
<syntaxhighlight lang="jq">"n product of divisors",
(range(1; 51) | "\(lpad(3)) \(product_of_divisors|lpad(15))")</lang>
(range(1; 51) | "\(lpad(3)) \(product_of_divisors|lpad(15))")</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,067: Line 1,067:
</pre>
</pre>
'''Example illustrating the use of gojq'''
'''Example illustrating the use of gojq'''
<lang jq>1234567890 | [., product_of_divisors]
<syntaxhighlight lang="jq">1234567890 | [., product_of_divisors]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,075: Line 1,075:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


function proddivisors(n)
function proddivisors(n)
Line 1,088: Line 1,088:
print(lpad(proddivisors(i), 10), i % 10 == 0 ? " \n" : "")
print(lpad(proddivisors(i), 10), i % 10 == 0 ? " \n" : "")
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
1 2 3 8 5 36 7 64 27 100
1 2 3 8 5 36 7 64 27 100
Line 1,097: Line 1,097:
</pre>
</pre>
One-liner version:
One-liner version:
<lang julia>proddivisors_oneliner(n) = prod(n%i==0 ? i : 1 for i in 1:n)</lang>
<syntaxhighlight lang="julia">proddivisors_oneliner(n) = prod(n%i==0 ? i : 1 for i in 1:n)</syntaxhighlight>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>import kotlin.math.pow
<syntaxhighlight lang="scala">import kotlin.math.pow


private fun divisorCount(n: Long): Long {
private fun divisorCount(n: Long): Long {
Line 1,142: Line 1,142:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Product of divisors for the first 50 positive integers:
<pre>Product of divisors for the first 50 positive integers:
Line 1,157: Line 1,157:


=={{header|MAD}}==
=={{header|MAD}}==
<lang MAD> NORMAL MODE IS INTEGER
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
DIMENSION D(50)
DIMENSION D(50)
THROUGH INIT, FOR I=1, 1, I.G.50
THROUGH INIT, FOR I=1, 1, I.G.50
Line 1,167: Line 1,167:
SHOW PRINT FORMAT F5, D(I), D(I+1), D(I+2), D(I+3), D(I+4)
SHOW PRINT FORMAT F5, D(I), D(I+1), D(I+2), D(I+3), D(I+4)
VECTOR VALUES F5 = $5(I10)*$
VECTOR VALUES F5 = $5(I10)*$
END OF PROGRAM </lang>
END OF PROGRAM </syntaxhighlight>
{{out}}
{{out}}
<pre> 1 2 3 8 5
<pre> 1 2 3 8 5
Line 1,181: Line 1,181:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>Divisors/*Apply[Times] /@ Range[50]</lang>
<syntaxhighlight lang="mathematica">Divisors/*Apply[Times] /@ Range[50]</syntaxhighlight>
{{out}}
{{out}}
<pre>{1, 2, 3, 8, 5, 36, 7, 64, 27, 100, 11, 1728, 13, 196, 225, 1024, 17, 5832, 19, 8000, 441, 484, 23, 331776, 125, 676, 729, 21952, 29, 810000, 31, 32768, 1089, 1156, 1225, 10077696, 37, 1444, 1521, 2560000, 41, 3111696, 43, 85184, 91125, 2116, 47, 254803968, 343, 125000}</pre>
<pre>{1, 2, 3, 8, 5, 36, 7, 64, 27, 100, 11, 1728, 13, 196, 225, 1024, 17, 5832, 19, 8000, 441, 484, 23, 331776, 125, 676, 729, 21952, 29, 810000, 31, 32768, 1089, 1156, 1225, 10077696, 37, 1444, 1521, 2560000, 41, 3111696, 43, 85184, 91125, 2116, 47, 254803968, 343, 125000}</pre>


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import math, strutils
<syntaxhighlight lang="nim">import math, strutils


func divisors(n: Positive): seq[int] =
func divisors(n: Positive): seq[int] =
Line 1,198: Line 1,198:
echo "Product of divisors for the first 50 positive numbers:"
echo "Product of divisors for the first 50 positive numbers:"
for n in 1..50:
for n in 1..50:
stdout.write ($prod(n.divisors)).align(10), if n mod 5 == 0: '\n' else: ' '</lang>
stdout.write ($prod(n.divisors)).align(10), if n mod 5 == 0: '\n' else: ' '</syntaxhighlight>


{{out}}
{{out}}
Line 1,214: Line 1,214:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Product_of_divisors
use strict; # https://rosettacode.org/wiki/Product_of_divisors
Line 1,224: Line 1,224:
$n % $_ or $products[$n] *= $_ for 1 .. $n;
$n % $_ or $products[$n] *= $_ for 1 .. $n;
}
}
printf '' . (('%11d' x 5) . "\n") x 10, @products[1 .. 50];</lang>
printf '' . (('%11d' x 5) . "\n") x 10, @products[1 .. 50];</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,241: Line 1,241:
=={{header|Phix}}==
=={{header|Phix}}==
=== imperative ===
=== imperative ===
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">50</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">50</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%,12d"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">product</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%,12d"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">product</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,262: Line 1,262:
=== functional ===
=== functional ===
same output
same output
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">50</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}),</span><span style="color: #7060A8;">product</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">50</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}),</span><span style="color: #7060A8;">product</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%,12d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">r</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%,12d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">r</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|Pike}}==
=={{header|Pike}}==
{{trans|Python}}
{{trans|Python}}


<lang Pike>int product_of_divisors(int n) {
<syntaxhighlight lang="pike">int product_of_divisors(int n) {
int ans, i, j;
int ans, i, j;
ans = i = j = 1;
ans = i = j = 1;
Line 1,298: Line 1,298:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,317: Line 1,317:
=={{header|Python}}==
=={{header|Python}}==
===Finding divisors efficiently===
===Finding divisors efficiently===
<lang Python>def product_of_divisors(n):
<syntaxhighlight lang="python">def product_of_divisors(n):
assert(isinstance(n, int) and 0 < n)
assert(isinstance(n, int) and 0 < n)
ans = i = j = 1
ans = i = j = 1
Line 1,330: Line 1,330:
if __name__ == "__main__":
if __name__ == "__main__":
print([product_of_divisors(n) for n in range(1,51)])</lang>
print([product_of_divisors(n) for n in range(1,51)])</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 2, 3, 8, 5, 36, 7, 64, 27, 100, 11, 1728, 13, 196, 225, 1024, 17, 5832, 19, 8000, 441, 484, 23, 331776, 125, 676, 729, 21952, 29, 810000, 31, 32768, 1089, 1156, 1225, 10077696, 37, 1444, 1521, 2560000, 41, 3111696, 43, 85184, 91125, 2116, 47, 254803968, 343, 125000]</pre>
<pre>[1, 2, 3, 8, 5, 36, 7, 64, 27, 100, 11, 1728, 13, 196, 225, 1024, 17, 5832, 19, 8000, 441, 484, 23, 331776, 125, 676, 729, 21952, 29, 810000, 31, 32768, 1089, 1156, 1225, 10077696, 37, 1444, 1521, 2560000, 41, 3111696, 43, 85184, 91125, 2116, 47, 254803968, 343, 125000]</pre>
Line 1,344: Line 1,344:
The goal of Rosetta code (see the landing page) is to provide contrastive '''insight''' (rather than comprehensive coverage of homework questions :-). Perhaps the scope for contrastive insight in the matter of ''divisors'' is already exhausted by the trivially different '''Proper divisors''' task.
The goal of Rosetta code (see the landing page) is to provide contrastive '''insight''' (rather than comprehensive coverage of homework questions :-). Perhaps the scope for contrastive insight in the matter of ''divisors'' is already exhausted by the trivially different '''Proper divisors''' task.


<lang python>'''Sums and products of divisors'''
<syntaxhighlight lang="python">'''Sums and products of divisors'''


from math import floor, sqrt
from math import floor, sqrt
Line 1,378: Line 1,378:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>


=={{header|Quackery}}==
=={{header|Quackery}}==
Line 1,384: Line 1,384:
<code>factors</code> is defined at [[Factors of an integer#Quackery]].
<code>factors</code> is defined at [[Factors of an integer#Quackery]].


<lang Quackery> [ 1 swap factors witheach * ] is product-of-divisors ( n --> n )
<syntaxhighlight lang="quackery"> [ 1 swap factors witheach * ] is product-of-divisors ( n --> n )
[] []
[] []
Line 1,391: Line 1,391:
witheach [ number$ nested join ]
witheach [ number$ nested join ]
75 wrap$
75 wrap$
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,401: Line 1,401:
=={{header|R}}==
=={{header|R}}==
This only takes one line.
This only takes one line.
<lang rsplus>sapply(1:50, function(n) prod(c(Filter(function(x) n %% x == 0, seq_len(n %/% 2)), n)))</lang>
<syntaxhighlight lang="rsplus">sapply(1:50, function(n) prod(c(Filter(function(x) n %% x == 0, seq_len(n %/% 2)), n)))</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
Yet more tasks that are tiny variations of each other. [[Tau function]], [[Tau number]], [[Sum of divisors]] and [[Product of divisors]] all use code with minimal changes. What the heck, post 'em all.
Yet more tasks that are tiny variations of each other. [[Tau function]], [[Tau number]], [[Sum of divisors]] and [[Product of divisors]] all use code with minimal changes. What the heck, post 'em all.


<lang perl6>use Prime::Factor:ver<0.3.0+>;
<syntaxhighlight lang="raku" line>use Prime::Factor:ver<0.3.0+>;
use Lingua::EN::Numbers;
use Lingua::EN::Numbers;


Line 1,423: Line 1,423:
say "\nDivisor products - first 100:\n", # ID
say "\nDivisor products - first 100:\n", # ID
(1..*).map({ [×] .&divisors })[^100]\ # the task
(1..*).map({ [×] .&divisors })[^100]\ # the task
.batch(5)».&comma».fmt("%16s").join("\n"); # display formatting</lang>
.batch(5)».&comma».fmt("%16s").join("\n"); # display formatting</syntaxhighlight>
{{out}}
{{out}}
<pre>Tau function - first 100:
<pre>Tau function - first 100:
Line 1,474: Line 1,474:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program displays the first N product of divisors (shown in a columnar format).*/
<syntaxhighlight lang="rexx">/*REXX program displays the first N product of divisors (shown in a columnar format).*/
numeric digits 20 /*ensure enough decimal digit precision*/
numeric digits 20 /*ensure enough decimal digit precision*/
parse arg n cols . /*obtain optional argument from the CL.*/
parse arg n cols . /*obtain optional argument from the CL.*/
Line 1,503: Line 1,503:
end /*k*/ /* [↑] % is the REXX integer division*/
end /*k*/ /* [↑] % is the REXX integer division*/
if k*k==x then return p * k /*Was X a square? If so, add √ x */
if k*k==x then return p * k /*Was X a square? If so, add √ x */
return p /*return (sigma) sum of the divisors. */</lang>
return p /*return (sigma) sum of the divisors. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 1,522: Line 1,522:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
limit = 50
limit = 50
row = 0
row = 0
Line 1,543: Line 1,543:


see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,562: Line 1,562:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|C++}}
{{trans|C++}}
<lang ruby>def divisor_count(n)
<syntaxhighlight lang="ruby">def divisor_count(n)
total = 1
total = 1
# Deal with powers of 2 first
# Deal with powers of 2 first
Line 1,598: Line 1,598:
print "\n"
print "\n"
end
end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>Product of divisors for the first 50 positive integers:
<pre>Product of divisors for the first 50 positive integers:
Line 1,614: Line 1,614:


=={{header|Verilog}}==
=={{header|Verilog}}==
<lang Verilog>module main;
<syntaxhighlight lang="verilog">module main;
integer p, n, i;
integer p, n, i;
Line 1,626: Line 1,626:
$finish ;
$finish ;
end
end
endmodule</lang>
endmodule</syntaxhighlight>


=={{header|VTL-2}}==
=={{header|VTL-2}}==
This sample only shows the divisor products of the first 20 numbers as (the original) VTL-2 only handles numbers in the range 0-65535. The divisor product of 24 would overflow.<br>
This sample only shows the divisor products of the first 20 numbers as (the original) VTL-2 only handles numbers in the range 0-65535. The divisor product of 24 would overflow.<br>
Note, all VTL-2 operators are single characters, however though the "<" operator does a lexx-than test, the ">" operator tests greater-than-or-equal.<br>
Note, all VTL-2 operators are single characters, however though the "<" operator does a lexx-than test, the ">" operator tests greater-than-or-equal.<br>
<lang VTL2>100 M=20
<syntaxhighlight lang="vtl2">100 M=20
110 I=0
110 I=0
120 I=I+1
120 I=I+1
Line 1,658: Line 1,658:
350 #=I/5*0+%=0=0*370
350 #=I/5*0+%=0=0*370
360 ?=""
360 ?=""
370 #=I<M*230</lang>
370 #=I<M*230</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,670: Line 1,670:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int, Nums
<syntaxhighlight lang="ecmascript">import "/math" for Int, Nums
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 1,677: Line 1,677:
Fmt.write("$9d ", Nums.prod(Int.divisors(i)))
Fmt.write("$9d ", Nums.prod(Int.divisors(i)))
if (i % 5 == 0) System.print()
if (i % 5 == 0) System.print()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,695: Line 1,695:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func ProdDiv(N); \Return product of divisors of N
<syntaxhighlight lang="xpl0">func ProdDiv(N); \Return product of divisors of N
int N, Prod, Div;
int N, Prod, Div;
[Prod:= 1;
[Prod:= 1;
Line 1,711: Line 1,711:
C:= C+1;
C:= C+1;
if rem(C/5) = 0 then CrLf(0)];
if rem(C/5) = 0 then CrLf(0)];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}