Product of divisors: Difference between revisions

m
(Added Quackery.)
m (→‎{{header|Wren}}: Minor tidy)
 
(32 intermediate revisions by 19 users not shown)
Line 11:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F product_of_divisors(n)
V ans = 1
V i = 1
Line 24:
R ans
 
print((1..50).map(n -> product_of_divisors(n)))</langsyntaxhighlight>
 
{{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>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
PROC ProdOfDivisors(INT n REAL POINTER prod)
INT i,j
REAL r
 
IntToReal(1,prod)
i=0
WHILE i*i<=n
DO
IF n MOD i=0 THEN
IntToReal(i,r)
RealMult(prod,r,prod)
j=n/i
IF j#i THEN
IntToReal(j,r)
RealMult(prod,r,prod)
FI
FI
i==+1
OD
RETURN
 
PROC Main()
BYTE i
REAL prod
 
Put(125) PutE() ;clear the screen
FOR i=1 TO 50
DO
ProdOfDivisors(i,prod)
PrintR(prod) Put(32)
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Product_of_divisors.png Screenshot from Atari 8-bit computer]
<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|ALGOL 68}}==
{{Trans|Fortran}}
<syntaxhighlight lang="algol68">BEGIN # product of divisors - transaltion of the Fortran sample #
[ 1 : 50 ]INT divis;
FOR i TO UPB divis DO divis[ i ] := 1 OD;
FOR i TO UPB divis DO
FOR j FROM i BY i TO UPB divis DO
divis[ j ] *:= i
OD
OD;
FOR i TO UPB divis DO
print( ( whole( divis[ i ], -10 ) ) );
IF i MOD 5 = 0 THEN print( ( newline ) ) FI
OD
END</syntaxhighlight>
{{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>
 
{{Trans|C++}}
<langsyntaxhighlight lang="algol68">BEGIN # find the product of the divisors of the first 100 positive integers #
# calculates the number of divisors of v #
PROC divisor count = ( INT v )INT:
Line 74 ⟶ 146:
OD
END
END</langsyntaxhighlight>
{{out}}
<pre>
Line 91 ⟶ 163:
 
=={{header|ALGOL W}}==
{{Trans|Fortran}}
<syntaxhighlight lang="algolw">begin % product of divisors - transaltion of the Fortran sample %
integer array divis ( 1 :: 50 );
for i := 1 until 50 do divis( i ) := 1;
for i := 1 until 50 do begin
for j := i step i until 50 do divis( j ) := divis( j ) * i
end for_i;
for i := 1 until 50 do begin
writeon( i_w := 10, s_w := 0, divis( i ) );
if i rem 5 = 0 then write()
end for_i
end.</syntaxhighlight>
{{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>
 
{{Trans|C++}}
<langsyntaxhighlight lang="algolw">begin % find the product of the divisors of the first 100 positive integers %
% calculates the number of divisors of v %
integer procedure divisor_count( integer value v ) ; begin
Line 136 ⟶ 234:
end for_n
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 151 ⟶ 249:
2116 47 254803968 343 125000
</pre>
 
=={{header|APL}}==
<langsyntaxhighlight APLlang="apl">divprod ← ×/(⍸0=⍳|⊢)
10 5 ⍴ divprod¨ ⍳50</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 8 5
Line 165 ⟶ 264:
41 3111696 43 85184 91125
2116 47 254803968 343 125000</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">loop split.every:5 to [:string] map 1..50 => [product factors &] 'line [
print map line 'i -> pad i 10
]</syntaxhighlight>
 
{{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>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRODUCT_OF_DIVISORS.AWK
# converted from Go
Line 197 ⟶ 315:
return(ans)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 209 ⟶ 327:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 N = 50
20 DIM D(N)
30 FOR I=1 TO N: D(I)=1: NEXT
Line 217 ⟶ 335:
70 NEXT J
80 NEXT I
90 FOR I=1 TO N: PRINT D(I),: NEXT</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 8 5
Line 230 ⟶ 348:
2116 47 2.54804E+08 343 125000</pre>
 
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">for n = 1 to 50
p = n
for i = 2 to n/2
if n mod i = 0 then p *= i
next i
if (n-1 mod 5) = 0 then print
print p; chr(9);
next n
end</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">OpenConsole()
For n.i = 1 To 50
p = n
For i.i = 2 To n/2
If n % i = 0 : p * i : EndIf
Next i
;If (n-1) % 5 = 0 : PrintN("") : EndIf
Print(Str(p) + #TAB$)
Next n
Input()
CloseConsole()</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">FOR n = 1 TO 50
p = n
FOR i = 2 TO n / 2
IF n MOD i = 0 THEN p = p * i
NEXT i
IF (n - 1) MOD 5 = 0 THEN PRINT
PRINT USING "###########"; p;
NEXT n
END</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">FOR n = 1 TO 50
LET p = n
FOR i = 2 TO n/2
IF MOD(n, i) = 0 THEN LET p = p * i
NEXT i
IF MOD(n-1, 5) = 0 THEN PRINT
PRINT p,
NEXT n
END</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">for n = 1 to 50
p = n
for i = 2 to n/2
if mod(n, i) = 0 then p = p * i : fi
next i
if mod(n-1, 5) = 0 then print : fi
print p using "###########";
next n
end</syntaxhighlight>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">(⊢(×´⊢/˜ 0=|˜ )1+↕)¨∘‿5⥊1+↕50</syntaxhighlight>
{{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>
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
 
Line 277 ⟶ 470:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Product of divisors for the first 50 positive integers:
Line 292 ⟶ 485:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cmath>
#include <iomanip>
#include <iostream>
Line 328 ⟶ 521:
std::cout << '\n';
}
}</langsyntaxhighlight>
 
{{out}}
Line 346 ⟶ 539:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
<lang Lisp>
(format t "~{~a ~}~%"
(loop for a from 1 to 100 collect
Line 352 ⟶ 545:
when (zerop (rem a b)) do (setf z (* z b))
finally (return z))))
</syntaxhighlight>
</lang>
{{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>
 
=={{header|Clojure}}==
{{trans|Raku}}
<syntaxhighlight lang="clojure">(require '[clojure.string :refer [join]])
(require '[clojure.pprint :refer [cl-format]])
 
(defn divisors [n] (filter #(zero? (rem n %)) (range 1 (inc n))))
 
(defn display-results [label per-line width nums]
(doall (map println (cons (str "\n" label ":") (list
(join "\n" (map #(join " " %)
(partition-all per-line
(map #(cl-format nil "~v:d" width %) nums)))))))))
 
(display-results "Tau function - first 100" 20 3
(take 100 (map (comp count divisors) (drop 1 (range)))))
 
(display-results "Tau numbers – first 100" 10 5
(take 100 (filter #(zero? (rem % (count (divisors %)))) (drop 1 (range)))))
 
(display-results "Divisor sums – first 100" 20 4
(take 100 (map #(reduce + (divisors %)) (drop 1 (range)))))
 
(display-results "Divisor products – first 100" 5 16
(take 100 (map #(reduce * (divisors %)) (drop 1 (range)))))
</syntaxhighlight>
{{Out}}<pre>
Tau function - first 100:
1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
4 4 2 8 3 4 4 6 2 8 2 6 4 4 4 9 2 4 4 8
2 8 2 6 6 4 2 10 3 6 4 6 2 8 4 8 4 4 2 12
2 4 6 7 4 8 2 6 4 8 2 12 2 4 6 6 4 8 2 10
5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
 
Tau numbers – first 100:
1 2 8 9 12 18 24 36 40 56
60 72 80 84 88 96 104 108 128 132
136 152 156 180 184 204 225 228 232 240
248 252 276 288 296 328 344 348 360 372
376 384 396 424 441 444 448 450 468 472
480 488 492 504 516 536 560 564 568 584
600 612 625 632 636 640 664 672 684 708
712 720 732 776 792 804 808 824 828 852
856 864 872 876 880 882 896 904 936 948
972 996 1,016 1,040 1,044 1,048 1,056 1,068 1,089 1,096
 
Divisor sums – first 100:
1 3 4 7 6 12 8 15 13 18 12 28 14 24 24 31 18 39 20 42
32 36 24 60 31 42 40 56 30 72 32 63 48 54 48 91 38 60 56 90
42 96 44 84 78 72 48 124 57 93 72 98 54 120 72 120 80 90 60 168
62 96 104 127 84 144 68 126 96 144 72 195 74 114 124 140 96 168 80 186
121 126 84 224 108 132 120 180 90 234 112 168 128 144 120 252 98 171 156 217
 
Divisor products – first 100:
1 2 3 8 5
36 7 64 27 100
11 1,728 13 196 225
1,024 17 5,832 19 8,000
441 484 23 331,776 125
676 729 21,952 29 810,000
31 32,768 1,089 1,156 1,225
10,077,696 37 1,444 1,521 2,560,000
41 3,111,696 43 85,184 91,125
2,116 47 254,803,968 343 125,000
2,601 140,608 53 8,503,056 3,025
9,834,496 3,249 3,364 59 46,656,000,000
61 3,844 250,047 2,097,152 4,225
18,974,736 67 314,432 4,761 24,010,000
71 139,314,069,504 73 5,476 421,875
438,976 5,929 37,015,056 79 3,276,800,000
59,049 6,724 83 351,298,031,616 7,225
7,396 7,569 59,969,536 89 531,441,000,000
8,281 778,688 8,649 8,836 9,025
782,757,789,696 97 941,192 970,299 1,000,000,000
</pre>
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. PRODUCT-OF-DIVISORS.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
03 DIVISOR-PRODUCTS PIC 9(9) OCCURS 50 TIMES.
03 NUM PIC 999.
03 MUL PIC 999.
01 OUTPUT-FORMAT.
03 NUM-OUT PIC Z(9)9.
03 LINE-PTR PIC 99 VALUE 1.
03 OUT-LINE PIC X(50) VALUE SPACES.
 
PROCEDURE DIVISION.
BEGIN.
PERFORM INIT VARYING NUM FROM 1 BY 1
UNTIL NUM IS GREATER THAN 50.
PERFORM CALCULATE-MULTIPLES VARYING MUL FROM 1 BY 1
UNTIL MUL IS GREATER THAN 50.
PERFORM OUTPUT-NUM VARYING NUM FROM 1 BY 1
UNTIL NUM IS GREATER THAN 50.
STOP RUN.
 
INIT.
MOVE 1 TO DIVISOR-PRODUCTS(NUM).
CALCULATE-MULTIPLES.
PERFORM MULTIPLY-NUM VARYING NUM FROM MUL BY MUL
UNTIL NUM IS GREATER THAN 50.
MULTIPLY-NUM.
MULTIPLY MUL BY DIVISOR-PRODUCTS(NUM).
 
OUTPUT-NUM.
MOVE DIVISOR-PRODUCTS(NUM) TO NUM-OUT.
STRING NUM-OUT DELIMITED BY SIZE INTO OUT-LINE
WITH POINTER LINE-PTR.
IF LINE-PTR IS EQUAL TO 51,
DISPLAY OUT-LINE,
MOVE SPACES TO OUT-LINE,
MOVE 1 TO LINE-PTR.</syntaxhighlight>
{{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>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub divprod(n: uint32): (prod: uint32) is
Line 382 ⟶ 705:
end if;
n := n + 1;
end loop;</langsyntaxhighlight>
 
{{out}}
Line 399 ⟶ 722:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.math;
import std.stdio;
 
Line 437 ⟶ 760:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Product of divisors for the first 50positive integers:
Line 450 ⟶ 773:
41 3111696 43 85184 91125
2116 47 254803968 343 125000</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
This is anohter example of building hierarchial libraries of subroutines. Rather than pack all the code inside a single block of code, this code is broken into subroutines that can be reused, saving time designing and test code.
 
<syntaxhighlight lang="Delphi">
{These subroutines would normally be in a library, but they are shown here for clarity.}
 
 
function GetAllProperDivisors(N: Integer;var IA: TIntegerDynArray): integer;
{Make a list of all the "proper dividers" for N}
{Proper dividers are the of numbers the divide evenly into N}
var I: integer;
begin
SetLength(IA,0);
for I:=1 to N-1 do
if (N mod I)=0 then
begin
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=I;
end;
Result:=Length(IA);
end;
 
 
function GetAllDivisors(N: Integer;var IA: TIntegerDynArray): integer;
{Make a list of all the "proper dividers" for N, Plus N itself}
begin
Result:=GetAllProperDivisors(N,IA)+1;
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=N;
end;
 
 
 
procedure ProductOfDivisors(Memo: TMemo);
var I,J,P: integer;
var IA: TIntegerDynArray;
var S: string;
begin
S:='';
for I:=1 to 50 do
begin
GetAllDivisors(I,IA);
P:=1;
for J:=0 to High(IA) do P:=P * IA[J];
S:=S+Format('%12D',[P]);
If (I mod 5)=0 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
end;
 
</syntaxhighlight>
{{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
Elapsed Time: 1.418 ms.
</pre>
 
 
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: grouping io math.primes.factors math.ranges prettyprint
sequences ;
 
"Product of divisors for the first 50 positive integers:" print
50 [1,b] [ divisors product ] map 5 group simple-table.</langsyntaxhighlight>
{{out}}
<pre>
Line 474 ⟶ 866:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran"> program divprod
implicit none
integer divis(50), i, j
Line 485 ⟶ 877:
write (*,'(I10)',advance='no') divis(i)
30 if (i/5 .ne. (i-1)/5) write (*,*)
end program</langsyntaxhighlight>
 
{{out}}
Line 499 ⟶ 891:
2116 47 254803968 343 125000</pre>
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">dim p as ulongint
for n as uinteger = 1 to 50
p = n
Line 507 ⟶ 899:
print p,
next n
</syntaxhighlight>
</lang>
{{out}}
<pre>1 2 3 8 5 36
Line 519 ⟶ 911:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 551 ⟶ 943:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 569 ⟶ 961:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">
10 FOR N = 1 TO 50
20 P# = N
Line 576 ⟶ 968:
50 NEXT I
60 PRINT P#,
70 NEXT N</langsyntaxhighlight>
{{out}}
<pre>
Line 586 ⟶ 978:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List.Split (chunksOf)
 
------------------------- DIVISORS -----------------------
Line 623 ⟶ 1,015:
 
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</langsyntaxhighlight>
{{Out}}
<pre>Sums of divisors of [1..100]:
Line 669 ⟶ 1,061:
782757789696 97 941192 970299 1000000000</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j"> {{ */ */@>,{ (^ i.@>:)&.>/ __ q: y }}@>:i.5 10x
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</syntaxhighlight>
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">public class ProductOfDivisors {
private static long divisorCount(long n) {
long total = 1;
Line 707 ⟶ 1,106:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Product of divisors for the first 50 positive integers:
Line 720 ⟶ 1,119:
41 3111696 43 85184 91125
2116 47 254803968 343 125000</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
gojq should be used if integer precision is to be guaranteed.
 
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.
<syntaxhighlight lang="jq"># divisors as an unsorted stream
def divisors:
if . == 1 then 1
else . as $n
| label $out
| range(1; $n) as $i
| ($i * $i) as $i2
| if $i2 > $n then break $out
else if $i2 == $n
then $i
elif ($n % $i) == 0
then $i, ($n/$i)
else empty
end
end
end;
 
def product(s): reduce s as $x (1; . * $x);
 
def product_of_divisors: product(divisors);
 
# For pretty-printing
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
</syntaxhighlight>
'''Example'''
<syntaxhighlight lang="jq">"n product of divisors",
(range(1; 51) | "\(lpad(3)) \(product_of_divisors|lpad(15))")</syntaxhighlight>
{{out}}
<pre>
n product of divisors
1 1
2 2
3 3
4 8
5 5
6 36
7 7
8 64
9 27
10 100
11 11
12 1728
13 13
14 196
15 225
16 1024
17 17
18 5832
19 19
20 8000
21 441
22 484
23 23
24 331776
25 125
26 676
27 729
28 21952
29 29
30 810000
31 31
32 32768
33 1089
34 1156
35 1225
36 10077696
37 37
38 1444
39 1521
40 2560000
41 41
42 3111696
43 43
44 85184
45 91125
46 2116
47 47
48 254803968
49 343
50 125000
</pre>
'''Example illustrating the use of gojq'''
<syntaxhighlight lang="jq">1234567890 | [., product_of_divisors]
</syntaxhighlight>
{{out}}
<pre>
[1234567890,157166308290967624614434966485493540963726721698403428784891012586974258380350906625255961242443130286157885664260857440235952354925000777353590796274952836151639520964606157865934675160485092641000000000000000000000000]
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function proddivisors(n)
Line 735 ⟶ 1,231:
print(lpad(proddivisors(i), 10), i % 10 == 0 ? " \n" : "")
end
</langsyntaxhighlight>{{out}}
<pre>
1 2 3 8 5 36 7 64 27 100
Line 743 ⟶ 1,239:
41 3111696 43 85184 91125 2116 47 254803968 343 125000
</pre>
One-liner version:
<syntaxhighlight lang="julia">proddivisors_oneliner(n) = prod(n%i==0 ? i : 1 for i in 1:n)</syntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import kotlin.math.pow
 
private fun divisorCount(n: Long): Long {
Line 787 ⟶ 1,285:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Product of divisors for the first 50 positive integers:
Line 802 ⟶ 1,300:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
DIMENSION D(50)
THROUGH INIT, FOR I=1, 1, I.G.50
Line 812 ⟶ 1,310:
SHOW PRINT FORMAT F5, D(I), D(I+1), D(I+2), D(I+3), D(I+4)
VECTOR VALUES F5 = $5(I10)*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre> 1 2 3 8 5
Line 824 ⟶ 1,322:
41 3111696 43 85184 91125
2116 47 254803968 343 125000</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Divisors/*Apply[Times] /@ Range[50]</syntaxhighlight>
{{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>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
divisorProduct = function(n)
ans = 1
i = 1
while i * i <= n
if n % i == 0 then
ans *= i
j = floor(n / i)
if j != i then ans *= j
end if
i += 1
end while
return ans
end function
 
products = []
for n in range(1,50)
products.push(divisorProduct(n))
end for
 
print products.join(", ")
</syntaxhighlight>
{{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>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
func divisors(n: Positive): seq[int] =
Line 838 ⟶ 1,369:
echo "Product of divisors for the first 50 positive numbers:"
for n in 1..50:
stdout.write ($prod(n.divisors)).align(10), if n mod 5 == 0: '\n' else: ' '</langsyntaxhighlight>
 
{{out}}
Line 854 ⟶ 1,385:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Product_of_divisors
Line 864 ⟶ 1,395:
$n % $_ or $products[$n] *= $_ for 1 .. $n;
}
printf '' . (('%11d' x 5) . "\n") x 10, @products[1 .. 50];</langsyntaxhighlight>
{{out}}
<pre>
Line 881 ⟶ 1,412:
=={{header|Phix}}==
=== imperative ===
<!--<langsyntaxhighlight Phixlang="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: #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;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 902 ⟶ 1,433:
=== functional ===
same output
<!--<langsyntaxhighlight Phixlang="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: #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>
<!--</langsyntaxhighlight>-->
 
=={{header|Pike}}==
{{trans|Python}}
 
<syntaxhighlight lang="pike">int product_of_divisors(int n) {
int ans, i, j;
ans = i = j = 1;
 
while(i * i <= n) {
if(n%i == 0) {
ans = ans * i;
j = n / i;
if(j != i) {
ans = ans * j;
}
}
i = i+1;
}
 
return ans;
}
 
int main() {
int limit = 50;
write("Product of divisors for the first " + (string)limit + " positive integers:\n");
for(int i = 1; i < limit + 1; i++) {
write("%11d", product_of_divisors(i));
if(i%5 == 0) {
write("\n");
}
}
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Product of divisors for the first 50 positive integers:
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|Python}}==
===Finding divisors efficiently===
<langsyntaxhighlight Pythonlang="python">def product_of_divisors(n):
assert(isinstance(n, int) and 0 < n)
ans = i = j = 1
Line 922 ⟶ 1,501:
if __name__ == "__main__":
print([product_of_divisors(n) for n in range(1,51)])</langsyntaxhighlight>
{{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>
Line 936 ⟶ 1,515:
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.
 
<langsyntaxhighlight lang="python">'''Sums and products of divisors'''
 
from math import floor, sqrt
Line 970 ⟶ 1,549:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
 
=={{header|Quackery}}==
Line 976 ⟶ 1,555:
<code>factors</code> is defined at [[Factors of an integer#Quackery]].
 
<langsyntaxhighlight Quackerylang="quackery"> [ 1 swap factors witheach * ] is product-of-divisors ( n --> n )
[] []
Line 983 ⟶ 1,562:
witheach [ number$ nested join ]
75 wrap$
</syntaxhighlight>
</lang>
 
{{out}}
Line 991 ⟶ 1,570:
1521 2560000 41 3111696 43 85184 91125 2116 47 254803968 343 125000</pre>
 
=={{header|R}}==
This only takes one line.
<syntaxhighlight lang="rsplus">sapply(1:50, function(n) prod(c(Filter(function(x) n %% x == 0, seq_len(n %/% 2)), n)))</syntaxhighlight>
 
=={{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.
 
<syntaxhighlight lang="raku" perl6line>use Prime::Factor:ver<0.3.0+>;
use Lingua::EN::Numbers;
 
Line 1,012 ⟶ 1,594:
say "\nDivisor products - first 100:\n", # ID
(1..*).map({ [×] .&divisors })[^100]\ # the task
.batch(5)».&comma».fmt("%16s").join("\n"); # display formatting</langsyntaxhighlight>
{{out}}
<pre>Tau function - first 100:
Line 1,063 ⟶ 1,645:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program displays the first N product of divisors (shown in a columnar format).*/
numeric digits 20 /*ensure enough decimal digit precision*/
parse arg n cols . /*obtain optional argument from the CL.*/
Line 1,081 ⟶ 1,663:
 
if $\=='' then say center(idx, 7)' ' $ /*any residuals sums left to display? */
say '───────┴'center("" , 102,'─') /* " " separator (above)*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 1,091 ⟶ 1,674:
end /*k*/ /* [↑] % is the REXX integer division*/
if k*k==x then return p * k /*Was X a square? If so, add √ x */
return p /*return (sigma) sum of the divisors. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,106 ⟶ 1,689:
41 │ 41 3,111,696 43 85,184 91,125
46 │ 2,116 47 254,803,968 343 125,000
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────
</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
limit = 50
row = 0
Line 1,130 ⟶ 1,714:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,145 ⟶ 1,729:
2116 47 254803968 343 125000
done...
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
DUP 1 1 ROT √ '''FOR''' k
OVER k
'''IF''' DUP2 MOD '''NOT'''
'''THEN''' DUP2 SQ ≠ ROT ROT '''IFTE''' *
'''ELSE''' DROP2 '''END'''
'''NEXT''' SWAP DROP
≫ ‘<span style="color:blue">PRODIV</span>’ STO
|
''( n -- div1 *..*divn )''
Initialize result and loop
Put n and k in stack
if k divides n then
multiply by n or k, depending on n ≠ k²
otherwise drop both n and k
get rid of n
|}
The following line of command delivers what is required:
≪ {} 1 50 '''FOR''' j j <span style="color:blue">PRODIV</span> + '''NEXT''' ≫ EVAL
{{out}}
<pre>
1: { 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|Ruby}}==
{{trans|C++}}
<langsyntaxhighlight lang="ruby">def divisor_count(n)
total = 1
# Deal with powers of 2 first
Line 1,185 ⟶ 1,801:
print "\n"
end
end</langsyntaxhighlight>
{{out}}
<pre>Product of divisors for the first 50 positive integers:
Line 1,198 ⟶ 1,814:
41 3111696 43 85184 91125
2116 47 254803968 343 125000</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">1..50 -> map { .divisors.prod }.say # simple
1..50 -> map {|n| isqrt(n**tau(n)) }.say # more efficient</syntaxhighlight>
{{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>
 
=={{header|Verilog}}==
<syntaxhighlight lang="verilog">module main;
integer p, n, i;
initial begin
for(n = 1; n <= 50; n=n+1)
begin
p = n;
for(i = 2; i <= n/2; i=i+1) if (n % i == 0) p = p * i;
$display(p);
end
$finish ;
end
endmodule</syntaxhighlight>
 
=={{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>
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>
<syntaxhighlight lang="vtl2">100 M=20
110 I=0
120 I=I+1
130 :I)=1
140 #=I<M*120
150 I=0
160 I=I+1
170 J=0
180 J=J+I
190 :J)=:J)*I
200 #=J<M*180
210 #=I<M*160
220 I=0
230 I=I+1
240 V=:I)
250 #=V>10*270
260 $=32
270 #=V>100*290
280 $=32
290 #=V>1000*310
300 $=32
310 #=V>10000*330
320 $=32
330 ?=:I)
340 $=32
350 #=I/5*0+%=0=0*370
360 ?=""
370 #=I<M*230</syntaxhighlight>
{{out}}
<pre>
1 2 3 8 5
36 7 64 27 100
11 1728 13 196 225
1024 17 5832 19 8000
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int, Nums
import "./fmt" for Fmt
 
System.print("The products of positive divisors for the first 50 positive integers are:")
Line 1,209 ⟶ 1,887:
Fmt.write("$9d ", Nums.prod(Int.divisors(i)))
if (i % 5 == 0) System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 1,224 ⟶ 1,902:
41 3111696 43 85184 91125
2116 47 254803968 343 125000
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func ProdDiv(N); \Return product of divisors of N
int N, Prod, Div;
[Prod:= 1;
for Div:= 2 to N do
if rem(N/Div) = 0 then
Prod:= Prod * Div;
return Prod;
];
 
int C, N;
[Format(10, 0);
C:= 0;
for N:= 1 to 50 do
[RlOut(0, float(ProdDiv(N)));
C:= C+1;
if rem(C/5) = 0 then CrLf(0)];
]</syntaxhighlight>
 
{{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>
9,476

edits