Product of divisors: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
(4 intermediate revisions by 4 users not shown)
Line 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}}==
Line 1,258 ⟶ 1,327:
{{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}}==
Line 1,648 ⟶ 1,745:
'''ELSE''' DROP2 '''END'''
'''NEXT''' SWAP DROP
≫ ‘<span style="color:blue">PRODIV</span>’ STO
≫ ‘PRODIV’ STO
|
''( n -- div1 *..*divn )''
Line 1,660 ⟶ 1,757:
|}
The following line of command delivers what is required:
≪ {} 1 50 '''FOR''' j j '''<span style="color:blue">PRODIV'''</span> + '''NEXT''' ≫ EVAL
{{out}}
<pre>
Line 1,717 ⟶ 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}}==
Line 1,775 ⟶ 1,880:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int, Nums
import "./fmt" for Fmt
 
System.print("The products of positive divisors for the first 50 positive integers are:")
9,485

edits