Product of divisors: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(6 intermediate revisions by 5 users not shown)
Line 549:
<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.
Line 699 ⟶ 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,184 ⟶ 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,558 ⟶ 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>
 
Line 1,612 ⟶ 1,815:
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,670 ⟶ 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,476

edits