Sum of divisors: Difference between revisions

Added Easylang
m (syntax highlighting fixup automation)
(Added Easylang)
 
(12 intermediate revisions by 7 users not shown)
Line 78:
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|C++}}...via Algol W
<syntaxhighlight lang="algol68">
BEGIN # sum the divisors of the first 100 positive integers #
 
# computes the sum of the divisors of v using the prime factorisation #
PROC divisor sum = ( INT v )INT:
BEGIN
INT total := 1, power := 2, n := v;
WHILE NOT ODD n DO # Deal with powers of 2 first #
total +:= power;
power *:= 2;
n OVERAB 2
OD;
INT p := 3; # Odd prime factors up to the square root #
WHILE ( p * p ) <= n DO
INT sum := 1;
power := p;
WHILE n MOD p = 0 DO
sum +:= power;
power *:= p;
n OVERAB p
OD;
p +:= 2;
total *:= sum
OD;
IF n > 1 THEN total *:= n + 1 FI; # If n > 1 then it's prime #
total
END # divisor sum # ;
BEGIN # show the first 100 divisor sums #
INT limit = 100;
print( ( "Sum of divisors for the first ", whole( limit, 0 ), " positive integers:" ) );
FOR n TO limit DO
IF n MOD 10 = 1 THEN print( ( newline ) ) FI;
print( ( " ", whole( divisor sum( n ), -4 ) ) )
OD
END
END
</syntaxhighlight>
{{out}}
<pre>
Sum of divisors for the first 100 positive integers:
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
</pre>
 
Line 529 ⟶ 583:
112 168 128 144 120 252 98 171 156 217
</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|CLU}}==
Line 770 ⟶ 898:
{$IFNDEF UNIX} readln; {$ENDIF}
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
write 1 & " "
for n = 2 to 100
p = 1 + n
for i = 2 to n div 2
if n mod i = 0
p += i
.
.
write p & " "
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 968 ⟶ 1,111:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sum_of_divisors}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
 
[[File:Fōrmulæ - Sum of divisors 01.png]]
 
'''Test case 1. Show the result for the first 100 positive integers'''
 
[[File:Fōrmulæ - Sum of divisors 02.png]]
 
[[File:Fōrmulæ - Sum of divisors 03.png]]
 
'''Test case 2. Char'''
 
[[File:Fōrmulæ - Sum of divisors 04.png]]
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Sum of divisors 05.png]]
In '''[https://formulae.org/?example=Sum_of_divisors this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Go}}==
Line 1,127 ⟶ 1,282:
8281 778688 8649 8836 9025
782757789696 97 941192 970299 1000000000</pre>
 
=={{header|J}}==
Brute force:
<syntaxhighlight lang=J> spd=: {{+/I.0=y|~i.1+y}}"0
spd 1+i.10 10
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
</syntaxhighlight>
 
Of course, there are [[j:Essays/Divisors#Sum_of_Divisors|other alternatives]].
 
=={{header|Java}}==
Line 1,305 ⟶ 1,478:
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217</pre>
 
=={{header|Lua}}==
{{Trans|C++}}...via Algol 68
<syntaxhighlight lang="lua">
do -- sum the divisors of the first 100 positive integers
 
-- computes the sum of the divisors of v using the prime factorisation
function divisor_sum( v )
local total, power, n = 1, 2, v
while n % 2 == 0 do -- Deal with powers of 2 first
total = total + power
power = power * 2
n = math.floor( n / 2 )
end
local p = 3 -- Odd prime factors up to the square root
while ( p * p ) <= n do
local sum = 1
power = p
while n % p == 0 do
sum = sum + power
power = power * p
n = math.floor( n / p )
end
p = p + 2
total = total * sum
end
if n > 1 then total = total * ( n + 1 ) end -- If n > 1 then it's prime
return total
end
 
-- show the first 100 divisor sums
local limit = 100
io.write( "Sum of divisors for the first ", limit, " positive integers:\n" )
for n = 1, limit do
io.write( string.format( " %4d", divisor_sum( n ) ) )
if n % 10 == 0 then io.write( "\n" ) end
end
 
end
</syntaxhighlight>
{{out}}
<pre>
Sum of divisors for the first 100 positive integers:
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
</pre>
 
=={{header|MAD}}==
Line 1,341 ⟶ 1,568:
{{out}}
<pre>{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}</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
divisorSum = function(n)
ans = 0
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
 
sums = []
for n in range(1, 100)
sums.push(divisorSum(n))
end for
 
print sums.join(", ")
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|Nim}}==
Line 1,899 ⟶ 2,154:
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
</pre>
 
=={{header|RPL}}==
{{trans|Python}}
{{works with|Halcyon Calc|4.2.8}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ → n
≪ 0
1 n √ '''FOR''' ii
'''IF''' n ii MOD NOT '''THEN'''
ii +
n ii / FLOOR
'''IF''' DUP ii ≠
'''THEN''' + '''ELSE''' DROP '''END'''
'''END NEXT'''
≫ ≫ ''''∑DIV'''' STO
|
'''∑DIV''' ''( n -- sum_of_divisors )''
ans = 0
while i*i <= n:
if 0 == n%i:
ans += i
j = n//i
if j != i:
ans += j
i += 1
return ans
|}
{{in}}
<pre>
≪ { } 1 100 FOR j j ∑DIV + NEXT ≫ EVAL
</pre>
{{out}}
<pre>
1: { 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 }
</pre>
 
Line 2,028 ⟶ 2,322:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int, Nums
import "./fmt" for Fmt
 
System.print("The sums of positive divisors for the first 100 positive integers are:")
2,016

edits