Sum of divisors: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Easylang)
 
(20 intermediate revisions by 13 users not shown)
Line 12: Line 12:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F sum_of_divisors(n)
<syntaxhighlight lang="11l">F sum_of_divisors(n)
V ans = 0
V ans = 0
V i = 1
V i = 1
Line 25: Line 25:
R ans
R ans


print((1..100).map(n -> sum_of_divisors(n)))</lang>
print((1..100).map(n -> sum_of_divisors(n)))</syntaxhighlight>


{{out}}
{{out}}
Line 33: Line 33:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>PROC PrintNum(BYTE x)
<syntaxhighlight lang="action!">PROC PrintNum(BYTE x)
Put(32)
Put(32)
IF x<10 THEN Put(32) FI
IF x<10 THEN Put(32) FI
Line 64: Line 64:


LMARGIN=oldLMARGIN ;restore left margin on the screen
LMARGIN=oldLMARGIN ;restore left margin on the screen
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_divisors.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_divisors.png Screenshot from Atari 8-bit computer]
Line 78: Line 78:
121 126 84 224 108 132 120 180 90 234
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
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>
</pre>


=={{header|ALGOL-M}}==
=={{header|ALGOL-M}}==
<lang algolm>begin
<syntaxhighlight lang="algolm">begin
integer N;
integer N;
N := 100;
N := 100;
Line 101: Line 155:
end;
end;
end;
end;
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
<pre> 1 3 4 7 6 12 8 15 13 18
Line 116: Line 170:
=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
{{Trans|C++}}
{{Trans|C++}}
<lang algolw>begin % sum the divisors of the first 100 positive integers %
<syntaxhighlight lang="algolw">begin % sum the divisors of the first 100 positive integers %
% computes the sum of the divisors of n using the prime %
% computes the sum of the divisors of n using the prime %
% factorisation %
% factorisation %
Line 155: Line 209:
end for_n
end for_n
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 173: Line 227:
=={{header|APL}}==
=={{header|APL}}==
{{works with|Dyalog APL}}
{{works with|Dyalog APL}}
<lang APL>10 10 ⍴ +/∘(⍸0=⍳|⊢)¨⍳100</lang>
<syntaxhighlight lang="apl">10 10 ⍴ +/∘(⍸0=⍳|⊢)¨⍳100</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
<pre> 1 3 4 7 6 12 8 15 13 18
Line 188: Line 242:
=={{header|AppleScript}}==
=={{header|AppleScript}}==


<lang applescript>on sumOfDivisors(n)
<syntaxhighlight lang="applescript">on sumOfDivisors(n)
if (n < 1) then return 0
if (n < 1) then return 0
set sum to 0
set sum to 0
Line 222: Line 276:
end task
end task


return task()</lang>
return task()</syntaxhighlight>


{{output}}
{{output}}
Line 230: Line 284:
62 96 104 127 84 144 68 126 96 144 72 195 74 114 124 140 96 168 80 186
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>
121 126 84 224 108 132 120 180 90 234 112 168 128 144 120 252 98 171 156 217</pre>

=={{header|Arturo}}==

<syntaxhighlight lang="rebol">loop split.every:10 map 1..100 'x -> sum factors x 'row [
print map row 'r -> pad to :string r 4
]</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|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUM_OF_DIVISORS.AWK
# syntax: GAWK -f SUM_OF_DIVISORS.AWK
# converted from Go
# converted from Go
Line 262: Line 335:
return(ans)
return(ans)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 279: Line 352:


=={{header|BASIC}}==
=={{header|BASIC}}==
<lang BASIC>10 DEFINT A-Z: DATA 100
<syntaxhighlight lang="basic">10 DEFINT A-Z: DATA 100
20 READ M
20 READ M
30 DIM D(M)
30 DIM D(M)
Line 285: Line 358:
50 FOR J=I TO M STEP I: D(J)=D(J)+I: NEXT
50 FOR J=I TO M STEP I: D(J)=D(J)+I: NEXT
60 PRINT D(I),
60 PRINT D(I),
70 NEXT</lang>
70 NEXT</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 3 4 7 6
<pre> 1 3 4 7 6
Line 307: Line 380:
112 168 128 144 120
112 168 128 144 120
252 98 171 156 217</pre>
252 98 171 156 217</pre>


==={{header|BASIC256}}===
<syntaxhighlight lang="freebasic">print 1; chr(9);
for n = 2 to 100
p = 1 + n
for i = 2 to n / 2
if n mod i = 0 then p += i
next i
print p; chr(9);
next n
end</syntaxhighlight>

==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">OpenConsole()
Print("1")
For n.i = 2 To 100
p = 1 + n
For i.i = 2 To n / 2
If Mod(n, i) = 0 : p + i : EndIf
Next i
Print(#TAB$ + Str(p))
Next n
Input()
CloseConsole()</syntaxhighlight>

==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">PRINT 1,
FOR n = 2 TO 100
p = 1 + n
FOR i = 2 TO n / 2
IF n MOD i = 0 THEN p = p + i
NEXT i
PRINT p,
NEXT n
END</syntaxhighlight>

==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">PRINT 1,
FOR n = 2 To 100
LET p = 1 + n
FOR i = 2 To n / 2
IF MOD(n, i) = 0 Then LET p = p + i
NEXT i
PRINT p,
NEXT n
END</syntaxhighlight>

==={{header|Yabasic}}===
<syntaxhighlight lang="freebasic">print 1,
for n = 2 to 100
p = 1 + n
for i = 2 to n / 2
if mod(n, i) = 0 then p = p + i : fi
next i
print p,
next n
end</syntaxhighlight>



=={{header|BCPL}}==
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


manifest $( MAXIMUM = 100 $)
manifest $( MAXIMUM = 100 $)
Line 337: Line 471:
if col rem 10 = 0 then wrch('*N')
if col rem 10 = 0 then wrch('*N')
$)
$)
$)</lang>
$)</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
<pre> 1 3 4 7 6 12 8 15 13 18
Line 352: Line 486:
=={{header|C}}==
=={{header|C}}==
{{trans|C++}}
{{trans|C++}}
<lang C>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


// See https://en.wikipedia.org/wiki/Divisor_function
// See https://en.wikipedia.org/wiki/Divisor_function
Line 388: Line 522:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
<pre>Sum of divisors for the first 100 positive integers:
Line 403: Line 537:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iomanip>
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <iostream>


Line 433: Line 567:
std::cout << '\n';
std::cout << '\n';
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 449: Line 583:
112 168 128 144 120 252 98 171 156 217
112 168 128 144 120 252 98 171 156 217
</pre>
</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}}==
=={{header|CLU}}==
<lang clu>% Calculate sum of divisors of positive integers up to and including N
<syntaxhighlight lang="clu">% Calculate sum of divisors of positive integers up to and including N
div_sums = proc (n: int) returns (array[int])
div_sums = proc (n: int) returns (array[int])
% Every number is at least divisible by 1
% Every number is at least divisible by 1
Line 474: Line 682:
if col // 10 = 0 then stream$putc(po, '\n') end
if col // 10 = 0 then stream$putc(po, '\n') end
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
<pre> 1 3 4 7 6 12 8 15 13 18
Line 486: Line 694:
121 126 84 224 108 132 120 180 90 234
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217</pre>
112 168 128 144 120 252 98 171 156 217</pre>

=={{header|Comal}}==
<syntaxhighlight lang="comal">0010 max#:=100
0020 //
0030 DIM divsum#(max#)
0040 FOR i#:=1 TO max# DO divsum#(i#):=1
0050 FOR i#:=2 TO max# DO FOR j#:=i# TO max# STEP i# DO divsum#(j#):+i#
0060 //
0070 ZONE 5
0080 FOR i#:=1 TO max# DO
0090 PRINT divsum#(i#),
0100 IF i# MOD 10=0 THEN PRINT
0110 ENDFOR i#
0120 END</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|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 494: Line 727:
when (zerop (rem a b))
when (zerop (rem a b))
sum b)))
sum b)))
</syntaxhighlight>
</lang>
{{out}}
{{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>
<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|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


const MAXIMUM := 100;
const MAXIMUM := 100;
Line 542: Line 775:
end if;
end if;
i := i + 1;
i := i + 1;
end loop;</lang>
end loop;</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
<pre> 1 3 4 7 6 12 8 15 13 18
Line 557: Line 790:
=={{header|D}}==
=={{header|D}}==
{{trans|C}}
{{trans|C}}
<lang d>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


// See https://en.wikipedia.org/wiki/Divisor_function
// See https://en.wikipedia.org/wiki/Divisor_function
Line 590: Line 823:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
<pre>Sum of divisors for the first 100 positive integers:
Line 607: Line 840:
{{libheader| System.SysUtils}}
{{libheader| System.SysUtils}}
{{Trans|Java}}
{{Trans|Java}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Sum_of_divisors;
program Sum_of_divisors;


Line 664: Line 897:


{$IFNDEF UNIX} readln; {$ENDIF}
{$IFNDEF UNIX} readln; {$ENDIF}
end.</lang>
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#}}==
=={{header|F_Sharp|F#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Sum of divisors. Nigel Galloway: March 9th., 2021
// Sum of divisors. Nigel Galloway: March 9th., 2021
let sod u=let P=primes32()
let sod u=let P=primes32()
Line 675: Line 923:
let n=Seq.head P in fG 1 n 1 1 n
let n=Seq.head P in fG 1 n 1 1 n
[1..100]|>Seq.iter(sod>>printf "%d "); printfn ""
[1..100]|>Seq.iter(sod>>printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 683: Line 931:
=={{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 ;


"Sum of divisors for the first 100 positive integers:" print
"Sum of divisors for the first 100 positive integers:" print
100 [1,b] [ divisors sum ] map 10 group simple-table.</lang>
100 [1,b] [ divisors sum ] map 10 group simple-table.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 704: Line 952:


=={{header|Fermat}}==
=={{header|Fermat}}==
<lang fermat>Func Sumdiv(n)=sm:=0;for i=1 to n do if Divides(i,n) then sm:=sm+i fi od; sm.
<syntaxhighlight lang="fermat">Func Sumdiv(n)=sm:=0;for i=1 to n do if Divides(i,n) then sm:=sm+i fi od; sm.
for i=1 to 100 do !!Sumdiv(i) od</lang>
for i=1 to 100 do !!Sumdiv(i) od</syntaxhighlight>


=={{header|FOCAL}}==
=={{header|FOCAL}}==
<lang FOCAL>01.10 S C=0
<syntaxhighlight lang="focal">01.10 S C=0
01.20 F I=1,100;S D(I)=1
01.20 F I=1,100;S D(I)=1
01.30 F I=2,100;F J=I,I,100;S D(J)=D(J)+I
01.30 F I=2,100;F J=I,I,100;S D(J)=D(J)+I
Line 718: Line 966:
02.30 I (9-C)2.4;R
02.30 I (9-C)2.4;R
02.40 T !
02.40 T !
02.50 S C=0</lang>
02.50 S C=0</syntaxhighlight>
{{out}}
{{out}}
<pre>= 1= 3= 4= 7= 6= 12= 8= 15= 13= 18
<pre>= 1= 3= 4= 7= 6= 12= 8= 15= 13= 18
Line 733: Line 981:
=={{header|Forth}}==
=={{header|Forth}}==
{{trans|C++}}
{{trans|C++}}
<lang forth>: divisor_sum ( n -- n )
<syntaxhighlight lang="forth">: divisor_sum ( n -- n )
1 >r
1 >r
2
2
Line 773: Line 1,021:


100 print_divisor_sums
100 print_divisor_sums
bye</lang>
bye</syntaxhighlight>


{{out}}
{{out}}
Line 791: Line 1,039:


=={{header|Fortran}}==
=={{header|Fortran}}==
<lang fortran> program DivSum
<syntaxhighlight lang="fortran"> program DivSum
implicit none
implicit none
integer i, j, col, divs(100)
integer i, j, col, divs(100)
Line 811: Line 1,059:
end if
end if
30 continue
30 continue
end program </lang>
end program </syntaxhighlight>
{{out}}
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
<pre> 1 3 4 7 6 12 8 15 13 18
Line 825: Line 1,073:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>dim p as ulongint
<syntaxhighlight lang="freebasic">dim p as ulongint
print 1,
print 1,
for n as uinteger = 2 to 100
for n as uinteger = 2 to 100
Line 833: Line 1,081:
next i
next i
print p,
print p,
next n</lang>
next n</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 3 4 7 6 12
<pre> 1 3 4 7 6 12
Line 852: Line 1,100:
112 168 128 144 120 252
112 168 128 144 120 252
98 171 156 217 </pre>
98 171 156 217 </pre>

=={{header|Frink}}==
<syntaxhighlight lang="frink">for n = 1 to 100
print[sum[allFactors[n]] + " "]</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|Fōrmulæ}}==
=={{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}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 894: Line 1,162:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 912: Line 1,180:


=={{header|GW-BASIC}}==
=={{header|GW-BASIC}}==
<lang gwbasic>
<syntaxhighlight lang="gwbasic">
5 PRINT 1,
5 PRINT 1,
10 FOR N = 2 TO 100
10 FOR N = 2 TO 100
Line 920: Line 1,188:
50 NEXT I
50 NEXT I
60 PRINT P,
60 PRINT P,
70 NEXT N</lang>
70 NEXT N</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
1 3 4 7 6 12 8 15 13 18 12
1 3 4 7 6 12 8 15 13 18 12
Line 934: Line 1,202:


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


------------------------- DIVISORS -----------------------
------------------------- DIVISORS -----------------------
Line 969: Line 1,237:


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 1,014: Line 1,282:
8281 778688 8649 8836 9025
8281 778688 8649 8836 9025
782757789696 97 941192 970299 1000000000</pre>
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}}==
=={{header|Java}}==
{{trans|C++}}
{{trans|C++}}
<lang java>public class DivisorSum {
<syntaxhighlight lang="java">public class DivisorSum {
private static long divisorSum(long n) {
private static long divisorSum(long n) {
var total = 1L;
var total = 1L;
Line 1,050: Line 1,336:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
<pre>Sum of divisors for the first 100 positive integers:
Line 1,070: Line 1,356:
Since a "divisors" function is more likely to be generally useful than a "sum of divisors"
Since a "divisors" function is more likely to be generally useful than a "sum of divisors"
function, this entry implements the latter in terms of the former.
function, this entry implements the latter in terms of the former.
<syntaxhighlight lang="jq">
<lang jq>
# divisors as an unsorted stream
# divisors as an unsorted stream
def divisors:
def divisors:
Line 1,098: Line 1,384:


# The task:
# The task:
[range(1; 101) | sum_of_divisors] | nwise(10) | map(lpad(4)) | join("")</lang>
[range(1; 101) | sum_of_divisors] | nwise(10) | map(lpad(4)) | join("")</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,114: Line 1,400:


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


function sumdivisors(n)
function sumdivisors(n)
Line 1,127: Line 1,413:
print(rpad(sumdivisors(i), 5), i % 25 == 0 ? " \n" : "")
print(rpad(sumdivisors(i), 5), i % 25 == 0 ? " \n" : "")
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<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
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
Line 1,137: Line 1,423:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|C++}}
{{trans|C++}}
<lang scala>fun divisorSum(n: Long): Long {
<syntaxhighlight lang="scala">fun divisorSum(n: Long): Long {
var nn = n
var nn = n
var total = 1L
var total = 1L
Line 1,179: Line 1,465:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
<pre>Sum of divisors for the first 100 positive integers:
Line 1,192: Line 1,478:
121 126 84 224 108 132 120 180 90 234
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217</pre>
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}}==
=={{header|MAD}}==
<lang MAD> NORMAL MODE IS INTEGER
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
DIMENSION DIVSUM(100)
DIMENSION DIVSUM(100)
Line 1,211: Line 1,551:


VECTOR VALUES F = $10(I4)*$
VECTOR VALUES F = $10(I4)*$
END OF PROGRAM </lang>
END OF PROGRAM </syntaxhighlight>
{{out}}
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
<pre> 1 3 4 7 6 12 8 15 13 18
Line 1,225: Line 1,565:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>DivisorSigma[1, Range[100]]</lang>
<syntaxhighlight lang="mathematica">DivisorSigma[1, Range[100]]</syntaxhighlight>
{{out}}
{{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>
<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}}==
=={{header|Nim}}==
<lang Nim>import math, strutils
<syntaxhighlight lang="nim">import math, strutils


proc divisors(n: Positive): seq[int] =
proc divisors(n: Positive): seq[int] =
Line 1,240: Line 1,608:


for n in 1..100:
for n in 1..100:
stdout.write ($sum(n.divisors)).align(3), if (n + 1) mod 10 == 0: '\n' else: ' '</lang>
stdout.write ($sum(n.divisors)).align(3), if (n + 1) mod 10 == 0: '\n' else: ' '</syntaxhighlight>


{{out}}
{{out}}
Line 1,258: Line 1,626:
Brute force version.Checking all divisors up to sqrt(n). More clever is [[Sum_of_divisors#Delphi]].But why not a different version.<BR>
Brute force version.Checking all divisors up to sqrt(n). More clever is [[Sum_of_divisors#Delphi]].But why not a different version.<BR>
Runs with gpc too.//cardinal is 32 or 64 bit depending on OS-System
Runs with gpc too.//cardinal is 32 or 64 bit depending on OS-System
<lang pascal>
<syntaxhighlight lang="pascal">
program Sum_of_divisors;
program Sum_of_divisors;
{$IFDEF WINDOWS}}
{$IFDEF WINDOWS}}
Line 1,308: Line 1,676:
readln;
readln;
{$ENDIF}
{$ENDIF}
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,320: Line 1,688:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,329: Line 1,697:


say "Divisor sums - first 100:\n" .
say "Divisor sums - first 100:\n" .
((sprintf "@{['%4d' x 100]}", @x[0..100-1]) =~ s/(.{80})/$1\n/gr);</lang>
((sprintf "@{['%4d' x 100]}", @x[0..100-1]) =~ s/(.{80})/$1\n/gr);</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18 12 28 14 24 24 31 18 39 20 42
<pre> 1 3 4 7 6 12 8 15 13 18 12 28 14 24 24 31 18 39 20 42
Line 1,338: Line 1,706:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>vector(100,X,sigma(X))</lang>
<syntaxhighlight lang="parigp">vector(100,X,sigma(X))</syntaxhighlight>
{{out}}<pre>
{{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>
[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 1,344: Line 1,712:
=={{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;">100</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;">100</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;">"%4d"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">sum</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;">"%4d"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">sum</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;">10</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;">10</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,365: Line 1,733:
=== functional ===
=== functional ===
same output
same output
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<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;">100</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}),</span><span style="color: #7060A8;">sum</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;">100</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}),</span><span style="color: #7060A8;">sum</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;">"%4d"</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;">10</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;">"%4d"</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;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
=== inline assembly ===
=== inline assembly ===
just to show it can be done, not that many would want to, same output
just to show it can be done, not that many would want to, same output
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</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;">100</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;">100</span> <span style="color: #008080;">do</span>
Line 1,405: Line 1,773:
<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;">10</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;">10</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>-->


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de propdiv (N)
<syntaxhighlight lang="picolisp">(de propdiv (N)
(let S 0
(let S 0
(for X N
(for X N
Line 1,416: Line 1,784:
(do 10
(do 10
(prin (align 4 (propdiv (inc (0))))) )
(prin (align 4 (propdiv (inc (0))))) )
(prinl) )</lang>
(prinl) )</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,432: Line 1,800:


=={{header|PL/M}}==
=={{header|PL/M}}==
<lang plm>100H:
<syntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 1,484: Line 1,852:
END;
END;
CALL EXIT;
CALL EXIT;
EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
<pre> 1 3 4 7 6 12 8 15 13 18
Line 1,499: Line 1,867:
=={{header|Python}}==
=={{header|Python}}==
===Using prime factorization===
===Using prime factorization===
<lang Python>def factorize(n):
<syntaxhighlight lang="python">def factorize(n):
assert(isinstance(n, int))
assert(isinstance(n, int))
if n < 0:
if n < 0:
Line 1,531: Line 1,899:
if __name__ == "__main__":
if __name__ == "__main__":
print([sum_of_divisors(n) for n in range(1,101)])</lang>
print([sum_of_divisors(n) for n in range(1,101)])</syntaxhighlight>


===Finding divisors efficiently===
===Finding divisors efficiently===
<lang Python>def sum_of_divisors(n):
<syntaxhighlight lang="python">def sum_of_divisors(n):
assert(isinstance(n, int) and 0 < n)
assert(isinstance(n, int) and 0 < n)
ans, i, j = 0, 1, 1
ans, i, j = 0, 1, 1
Line 1,547: Line 1,915:
if __name__ == "__main__":
if __name__ == "__main__":
print([sum_of_divisors(n) for n in range(1,101)])</lang>
print([sum_of_divisors(n) for n in range(1,101)])</syntaxhighlight>
{{out}}
{{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>
<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>
Line 1,561: Line 1,929:
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,595: Line 1,963:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>


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


<lang Quackery> [ 0 swap factors witheach + ] is sum-of-divisors
<syntaxhighlight lang="quackery"> [ 0 swap factors witheach + ] is sum-of-divisors


[] []
[] []
Line 1,607: Line 1,975:
[ i^ 1+ sum-of-divisors join ]
[ i^ 1+ sum-of-divisors join ]
witheach [ number$ nested join ]
witheach [ number$ nested join ]
72 wrap$ </lang>
72 wrap$ </syntaxhighlight>


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


=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>#lang racket/base
<syntaxhighlight lang="racket">#lang racket/base


(require math/number-theory)
(require math/number-theory)
Line 1,630: Line 1,998:
(define (sum-of-divisors n) (apply + (divisors n)))
(define (sum-of-divisors n) (apply + (divisors n)))


(displayln (for/list ((n (in-range 1 101))) (sum-of-divisors n)))</lang>
(displayln (for/list ((n (in-range 1 101))) (sum-of-divisors n)))</syntaxhighlight>


{{out}}
{{out}}
Line 1,639: Line 2,007:
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,656: Line 2,024:
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,707: Line 2,075:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program displays the first N sum of divisors (shown in a columnar format). */
<syntaxhighlight lang="rexx">/*REXX program displays the first N sum of divisors (shown in a columnar format). */
parse arg n cols . /*obtain optional argument from the CL.*/
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/
Line 1,735: Line 2,103:
end /*k*/ /* [↑] % is the REXX integer division*/
end /*k*/ /* [↑] % is the REXX integer division*/
if k*k==x then return s + k /*Was X a square? If so, add √ x */
if k*k==x then return s + k /*Was X a square? If so, add √ x */
return s /*return (sigma) sum of the divisors. */</lang>
return s /*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,754: Line 2,122:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
see "the sums of divisors for 100 integers:" + nl
see "the sums of divisors for 100 integers:" + nl
num = 0
num = 0
Line 1,771: Line 2,139:
see "" + sum + " "
see "" + sum + " "
next
next
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,786: Line 2,154:
121 126 84 224 108 132 120 180 90 234
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
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>
</pre>


=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|C++}}
{{trans|C++}}
<lang ruby>def divisor_sum(n)
<syntaxhighlight lang="ruby">def divisor_sum(n)
total = 1
total = 1
power = 2
power = 2
Line 1,830: Line 2,237:
print "\n"
print "\n"
end
end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
<pre>Sum of divisors for the first 100 positive integers:
Line 1,845: Line 2,252:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>1..100 -> map { .sigma }.say</lang>
<syntaxhighlight lang="ruby">1..100 -> map { .sigma }.say</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,852: Line 2,259:


=={{header|Tiny BASIC}}==
=={{header|Tiny BASIC}}==
<lang tinybasic>
<syntaxhighlight lang="tinybasic">
PRINT 1
PRINT 1
LET N = 1
LET N = 1
Line 1,864: Line 2,271:
30 PRINT S
30 PRINT S
IF N < 100 THEN GOTO 10
IF N < 100 THEN GOTO 10
END</lang>
END</syntaxhighlight>

=={{header|Verilog}}==
<syntaxhighlight lang="verilog">module main;
integer n, p, i;
initial begin
$write("1");
for(n=2; n<=100; n=n+1) begin
p = 1 + n;
for(i=2; i<=n/2; i=i+1) if(n % i == 0) p = p + i;
$write(p);
end
$finish ;
end
endmodule</syntaxhighlight>

=={{header|VTL-2}}==
<syntaxhighlight lang="vtl2">10 C=0
20 M=100
30 I=1
40 :I)=0
50 I=I+1
60 #=M>I*40
70 I=1
80 J=I
90 :J)=:J)+I
100 J=J+I
110 #=M>J*90
120 ?=:I)
130 $=9
140 C=C+1
150 #=C/10*0+0<%*170
160 ?=""
170 I=I+1
180 #=M>I*80</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|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int, Nums
<syntaxhighlight lang="wren">import "./math" for Int, Nums
import "/fmt" for Fmt
import "./fmt" for Fmt


System.print("The sums of positive divisors for the first 100 positive integers are:")
System.print("The sums of positive divisors for the first 100 positive integers are:")
Line 1,876: Line 2,329:
Fmt.write("$3d ", Nums.sum(Int.divisors(i)))
Fmt.write("$3d ", Nums.sum(Int.divisors(i)))
if (i % 10 == 0) System.print()
if (i % 10 == 0) System.print()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,891: Line 2,344:
121 126 84 224 108 132 120 180 90 234
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
112 168 128 144 120 252 98 171 156 217
</pre>

=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func SumDiv(N); \Return sum of divisors of N
int N, Sum, Div;
[Sum:= 0;
for Div:= 1 to N do
if rem(N/Div) = 0 then
Sum:= Sum + Div;
return Sum;
];

int C, N;
[C:= 0;
for N:= 1 to 100 do
[IntOut(0, SumDiv(N));
C:= C+1;
if rem(C/10) then ChOut(0, 9\tab\) else CrLf(0)];
]</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>
</pre>

Latest revision as of 22:24, 17 February 2024

Sum of divisors is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Given a positive integer, sum its positive divisors.


Task

Show the result for the first 100 positive integers.



11l

Translation of: Python
F sum_of_divisors(n)
   V ans = 0
   V i = 1
   V j = 1
   L i * i <= n
      I 0 == n % i
         ans += i
         j = n I/ i
         I j != i
            ans += j
      i++
   R ans

print((1..100).map(n -> sum_of_divisors(n)))
Output:
[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]

Action!

PROC PrintNum(BYTE x)
  Put(32)
  IF x<10 THEN Put(32) FI
  IF x<100 THEN Put(32) FI
  PrintB(x)
RETURN

PROC Main()
  DEFINE MAX="100"
  BYTE ARRAY div(MAX+1)
  BYTE i,j,LMARGIN=$52,oldLMARGIN

  oldLMARGIN=LMARGIN
  LMARGIN=0 ;remove left margin on the screen
  Put(125) PutE() ;clear the screen

  SetBlock(div,MAX+1,1)
  FOR i=2 TO MAX
  DO
    FOR j=i TO MAX STEP i
    DO
      div(j)==+i
    OD
  OD

  FOR i=1 TO MAX
  DO
    PrintNum(div(i))
  OD

  LMARGIN=oldLMARGIN ;restore left margin on the screen
RETURN
Output:

Screenshot from Atari 8-bit computer

  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

ALGOL 68

Translation of: C++

...via Algol W

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
Output:
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

ALGOL-M

begin
    integer N;
    N := 100;
    begin
        integer array div[1:N];
        integer i, j, col;
        for i := 1 step 1 until N do div[i] := 1;
        for i := 2 step 1 until N do
            for j := i step i until N do
                div[j] := div[j] + i;
        
        col := 0;
        for i := 1 step 1 until N do begin
            if (col-1)/10 <> col/10 then
                write(div[i])
            else
                writeon(div[i]);
            col := col + 1;
        end;
    end;
end
Output:
     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

ALGOL W

Translation of: C++
begin % sum the divisors of the first 100 positive integers %
    % computes the sum of the divisors of n using the prime %
    % factorisation                                         %
    integer procedure divisor_sum( integer value v ) ; begin
        integer total, power, n, p;
        total := 1; power := 2; n := v;
        % Deal with powers of 2 first %
        while not odd( n ) do begin
            total := total + power;
            power := power * 2;
            n     := n div 2
        end while_not_odd_n ;
        % Odd prime factors up to the square root %
        p := 3;
        while ( p * p ) <= n do begin
            integer sum;
            sum   := 1;
            power := p;
            while n rem p = 0 do begin
                sum   := sum + power;
                power := power * p;
                n     := n div p
            end while_n_rem_p_eq_0 ;
            p     := p + 2;
            total := total * sum
        end while_p_x_p_le_n ;
        % If n > 1 then it's prime %
        if n > 1 then total := total * ( n + 1 );
        total
    end divisor_sum ;
    begin
        integer limit;
        limit := 100;
        write( i_w := 1, s_w := 0, "Sum of divisors for the first ", limit, " positive integers:" );
        for n := 1 until limit do begin
            if n rem 10 = 1 then write();
            writeon( i_w := 4, s_w := 1, divisor_sum( n ) )
        end for_n
    end
end.
Output:
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

APL

Works with: Dyalog APL
10 10  +/∘(0=⍳|⊢)¨100
Output:
  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

AppleScript

on sumOfDivisors(n)
    if (n < 1) then return 0
    set sum to 0
    set sqrt to n ^ 0.5
    set limit to sqrt div 1
    if (limit = sqrt) then
        set sum to sum + limit
        set limit to limit - 1
    end if
    repeat with i from 1 to limit
        if (n mod i is 0) then set sum to sum + i + n div i
    end repeat
    
    return sum
end sumOfDivisors

on task()
    set output to {}
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to ""
    repeat with i from 0 to 80 by 20
        set thisLine to {}
        repeat with j from 1 to 20
            set end of thisLine to text -4 thru -1 of ("   " & sumOfDivisors(i + j))
        end repeat
        set end of output to thisLine as text
    end repeat
    set AppleScript's text item delimiters to linefeed
    set output to output as text
    set AppleScript's text item delimiters to astid
    
    return output
end task

return task()
Output:
   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

Arturo

loop split.every:10 map 1..100 'x -> sum factors x 'row [
    print map row 'r -> pad to :string r 4
]
Output:
   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

AWK

# syntax: GAWK -f SUM_OF_DIVISORS.AWK
# converted from Go
BEGIN {
    limit = 100
    printf("The sums of positive divisors for the first %d positive integers are:\n",limit)
    for (i=1; i<=limit; i++) {
      printf("%3d ",sum_divisors(i))
      if (i % 10 == 0) {
        printf("\n")
      }
    }
    exit(0)
}
function sum_divisors(n,  ans,i,j,k) {
    ans = 0
    i = 1
    k = (n % 2 == 0) ? 1 : 2
    while (i*i <= n) {
      if (n % i == 0) {
        ans += i
        j = n / i
        if (j != i) {
          ans += j
        }
      }
      i += k
    }
    return(ans)
}
Output:
The sums of positive divisors for the first 100 positive integers are:
  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

BASIC

10 DEFINT A-Z: DATA 100
20 READ M
30 DIM D(M)
40 FOR I=1 TO M
50 FOR J=I TO M STEP I: D(J)=D(J)+I: NEXT
60 PRINT D(I),
70 NEXT
Output:
 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


BASIC256

print 1; chr(9);
for n = 2 to 100
	p = 1 + n
	for i = 2 to n / 2
		if n mod i = 0 then p += i
	next i
	print p; chr(9);
next n
end

PureBasic

OpenConsole()
Print("1")
For n.i = 2 To 100
  p = 1 + n
  For i.i = 2 To n / 2
    If Mod(n, i) = 0 : p + i : EndIf
  Next i
  Print(#TAB$ + Str(p))
Next n
Input()
CloseConsole()

QBasic

Works with: QBasic version 1.1
Works with: QuickBasic version 4.5
PRINT 1,
FOR n = 2 TO 100
    p = 1 + n
    FOR i = 2 TO n / 2
        IF n MOD i = 0 THEN p = p + i
    NEXT i
    PRINT p,
NEXT n
END

True BASIC

PRINT 1,
FOR n = 2 To 100
    LET p = 1 + n
    FOR i = 2 To n / 2
        IF MOD(n, i) = 0 Then LET p = p + i
    NEXT i
    PRINT p,
NEXT n
END

Yabasic

print 1,
for n = 2 to 100
    p = 1 + n
    for i = 2 to n / 2
        if mod(n, i) = 0 then p = p + i : fi
    next i
    print p,
next n
end


BCPL

get "libhdr"

manifest $( MAXIMUM = 100 $)

// Calculate sum of divisors of positive integers up to N inclusive
let sumdivs(v, n) be
$(  for i=1 to n do v!i := 1 // All numbers are divisible by 1
    for i=2 to n do
    $(  let j = i
        while j<=n do
        $(  v!j := v!j + i   // Every multiple of i is divisible by i
            j := j + i
        $)
    $)
$)

// Print sum of divisors from 1 to MAXIMUM
let start() be
$(  let divsum = vec MAXIMUM
    let col = 0
    
    sumdivs(divsum, MAXIMUM)

    for i = 1 to MAXIMUM do
    $(  writed(divsum!i, 5)
        col := col + 1
        if col rem 10 = 0 then wrch('*N')
    $)
$)
Output:
    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

C

Translation of: C++
#include <stdio.h>

// See https://en.wikipedia.org/wiki/Divisor_function
unsigned int divisor_sum(unsigned int n) {
    unsigned int total = 1, power = 2;
    unsigned int p;
    // Deal with powers of 2 first
    for (; (n & 1) == 0; power <<= 1, n >>= 1) {
        total += power;
    }
    // Odd prime factors up to the square root
    for (p = 3; p * p <= n; p += 2) {
        unsigned int sum = 1;
        for (power = p; n % p == 0; power *= p, n /= p) {
            sum += power;
        }
        total *= sum;
    }
    // If n > 1 then it's prime
    if (n > 1) {
        total *= n + 1;
    }
    return total;
}

int main() {
    const unsigned int limit = 100;
    unsigned int n;
    printf("Sum of divisors for the first %d positive integers:\n", limit);
    for (n = 1; n <= limit; ++n) {
        printf("%4d", divisor_sum(n));
        if (n % 10 == 0) {
            printf("\n");
        }
    }
    return 0;
}
Output:
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

C++

#include <iomanip>
#include <iostream>

// See https://en.wikipedia.org/wiki/Divisor_function
unsigned int divisor_sum(unsigned int n) {
    unsigned int total = 1, power = 2;
    // Deal with powers of 2 first
    for (; (n & 1) == 0; power <<= 1, n >>= 1)
        total += power;
    // Odd prime factors up to the square root
    for (unsigned int p = 3; p * p <= n; p += 2) {
        unsigned int sum = 1;
        for (power = p; n % p == 0; power *= p, n /= p)
            sum += power;
        total *= sum;
    }
    // If n > 1 then it's prime
    if (n > 1)
        total *= n + 1;
    return total;
}

int main() {
    const unsigned int limit = 100;
    std::cout << "Sum of divisors for the first " << limit << " positive integers:\n";
    for (unsigned int n = 1; n <= limit; ++n) {
        std::cout << std::setw(4) << divisor_sum(n);
        if (n % 10 == 0)
            std::cout << '\n';
    }
}
Output:
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

Clojure

Translation of: Raku
(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)))))
Output:
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

CLU

% Calculate sum of divisors of positive integers up to and including N
div_sums = proc (n: int) returns (array[int]) 
    % Every number is at least divisible by 1
    ds: array[int] := array[int]$fill(1, n, 1)
    
    for i: int in int$from_to(2, n) do
        for j: int in int$from_to_by(i, n, i) do
            ds[j] := ds[j] + i  % every multiple of i is divisible by i
        end
    end
    return (ds)
end div_sums

% Print sum of divisors from 1 to 100
start_up = proc ()
    po: stream := stream$primary_output()
    
    col: int := 0
    for i: int in array[int]$elements(div_sums(100)) do
        stream$putright(po, int$unparse(i), 5)
        col := col + 1
        if col // 10 = 0 then stream$putc(po, '\n') end
    end
end start_up
Output:
    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

Comal

0010 max#:=100
0020 //
0030 DIM divsum#(max#)
0040 FOR i#:=1 TO max# DO divsum#(i#):=1
0050 FOR i#:=2 TO max# DO FOR j#:=i# TO max# STEP i# DO divsum#(j#):+i#
0060 //
0070 ZONE 5
0080 FOR i#:=1 TO max# DO
0090   PRINT divsum#(i#),
0100   IF i# MOD 10=0 THEN PRINT
0110 ENDFOR i#
0120 END
Output:
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

Common Lisp

(format t "~{~a ~}~%"
        (loop for a from 1 to 100 collect
              (loop for b from 1 to a
                    when (zerop (rem a b))
                    sum b)))
Output:
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 

Cowgol

include "cowgol.coh";

const MAXIMUM := 100;
typedef N is int(0, MAXIMUM+1);

sub print_col(n: N, colsize: uint8) is
    var buf: uint8[32];
    var nsize := UIToA(n as uint32, 10, &buf[0]) - &buf[0];
    while colsize > nsize as uint8 loop
        print_char(' ');
        colsize := colsize - 1;
    end loop;
    print(&buf[0]);
end sub;

var divsum: N[MAXIMUM+1];
var i: N := 1;

while i <= MAXIMUM loop
    divsum[i] := 1;
    i := i + 1;
end loop;

i := 2;
while i <= MAXIMUM loop
    var j := i;
    while j <= MAXIMUM loop
        divsum[j] := divsum[j] + i;
        j := j + i;
    end loop;
    i := i + 1;
end loop;

var col: uint8 := 0;
i := 1;
while i <= MAXIMUM loop
    print_col(divsum[i], 5);
    col := col + 1;
    if col == 10 then
        print_nl();
        col := 0;
    end if;
    i := i + 1;
end loop;
Output:
    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

D

Translation of: C
import std.stdio;

// See https://en.wikipedia.org/wiki/Divisor_function
uint divisor_sum(uint n) {
    uint total = 1, power = 2;
    // Deal with powers of 2 first
    for (; (n & 1) == 0; power <<= 1, n >>= 1) {
        total += power;
    }
    // Odd prime factors up to the square root
    for (uint p = 3; p * p <= n; p += 2) {
        uint sum = 1;
        for (power = p; n % p == 0; power *= p, n /= p) {
            sum += power;
        }
        total *= sum;
    }
    // If n > 1 then it's prime
    if (n > 1) {
        total *= n + 1;
    }
    return total;
}

void main() {
    immutable limit = 100;
    writeln("Sum of divisors for the first ", limit," positive integers:");
    for (uint n = 1; n <= limit; ++n) {
        writef("%4d", divisor_sum(n));
        if (n % 10 == 0) {
            writeln;
        }
    }
}
Output:
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      

Delphi

Translation of: Java
program Sum_of_divisors;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

function DivisorSum(n: Cardinal): Cardinal;
var
  total, power, p, sum: Cardinal;
begin
  total := 1;
  power := 2;

  // Deal with powers of 2 first

  while (n and 1 = 0) do
  begin
    inc(total, power);
    power := power shl 1;
    n := n shr 1;
  end;

  // Odd prime factors up to the square root
  p := 3;
  while p * p <= n do
  begin
    sum := 1;
    power := p;
    while n mod p = 0 do
    begin
      inc(sum, power);
      power := power * p;
      n := n div p;
    end;
    total := total * sum;
    inc(p, 2);
  end;

  // If n > 1 then it's prime
  if n > 1 then
    total := total * (n + 1);
  Result := total;
end;

begin
  const limit = 100;
  writeln('Sum of divisors for the first ', limit, ' positive integers:');
  for var n := 1 to limit do
  begin
    Write(divisorSum(n): 8);
    if n mod 10 = 0 then
      writeln;
  end;

 {$IFNDEF UNIX} readln; {$ENDIF}
end.

EasyLang

Translation of: BASIC256
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 & " "
.

F#

This task uses Extensible Prime Generator (F#).

// Sum of divisors. Nigel Galloway: March 9th., 2021
let sod u=let P=primes32()
          let rec fN g=match u%g with 0->g |_->fN(Seq.head P)
          let rec fG n i g e l=match n=u,u%l with (true,_)->e*g |(_,0)->fG (n*i) i g (e+l)(l*i) |_->let q=fN(Seq.head P) in fG n q (g*e) 1 q
          let n=Seq.head P in fG 1 n 1 1 n
[1..100]|>Seq.iter(sod>>printf "%d "); printfn ""
Output:
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

Factor

Works with: Factor version 0.99 2020-08-14
USING: grouping io math.primes.factors math.ranges prettyprint
sequences ;

"Sum of divisors for the first 100 positive integers:" print
100 [1,b] [ divisors sum ] map 10 group simple-table.
Output:
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

Fermat

Func Sumdiv(n)=sm:=0;for i=1 to n do if Divides(i,n) then sm:=sm+i fi od; sm.
for i=1 to 100 do !!Sumdiv(i) od

FOCAL

01.10 S C=0
01.20 F I=1,100;S D(I)=1
01.30 F I=2,100;F J=I,I,100;S D(J)=D(J)+I
01.40 F I=1,100;D 2
01.50 Q

02.10 T %4,D(I)
02.20 S C=C+1
02.30 I (9-C)2.4;R
02.40 T !
02.50 S C=0
Output:
=    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

Forth

Translation of: C++
: divisor_sum ( n -- n )
  1 >r
  2
  begin
    over 2 mod 0=
  while
    dup r> + >r
    2*
    swap 2/ swap
  repeat
  drop
  3
  begin
    2dup dup * >=
  while
    dup
    1 >r
    begin
      2 pick 2 pick mod 0=
    while
      dup r> + >r
      over * >r
      tuck / swap
      r>
    repeat
    2r> * >r
    drop
    2 +
  repeat
  drop
  dup 1 > if 1+ r> * else drop r> then ;

: print_divisor_sums ( n -- )
  ." Sum of divisors for the first " dup . ." positive integers:" cr
  1+ 1 do
    i divisor_sum 4 .r
    i 10 mod 0= if cr else space then
  loop ;

100 print_divisor_sums
bye
Output:
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

Fortran

       program DivSum
       implicit none
       integer i, j, col, divs(100)
       
       do 10 i=1, 100, 1
 10        divs(i) = 1
 
       do 20 i=2, 100, 1
           do 20 j=i, 100, i
 20            divs(j) = divs(j) + i
 
       col = 0
       do 30 i=1, 100, 1
           write (*,'(I4)',advance='no') divs(i)
           col = col + 1
           if (col .eq. 10) then
               col = 0
               write (*,*)
           end if
 30    continue
       end program
Output:
   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

FreeBASIC

dim p as ulongint
print 1,
for n as uinteger = 2 to 100
    p = 1+n
    for i as uinteger = 2 to n/2
        if n mod i = 0 then p += i
    next i
    print p,
next n
Output:
  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 

Frink

for n = 1 to 100
   print[sum[allFactors[n]] + " "]
Output:
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 

Fōrmulæ

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 —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.

Programs in Fōrmulæ are created/edited online in its website.

In this page you can see and run the program(s) related to this task and their results. You can also change either the programs or the parameters they are called with, for experimentation, but remember that these programs were created with the main purpose of showing a clear solution of the task, and they generally lack any kind of validation.

Solution

Test case 1. Show the result for the first 100 positive integers

Test case 2. Char

Go

package main

import "fmt"

func sumDivisors(n int) int {
    sum := 0
    i := 1
    k := 2
    if n%2 == 0 {
        k = 1
    }
    for i*i <= n {
        if n%i == 0 {
            sum += i
            j := n / i
            if j != i {
                sum += j
            }
        }
        i += k
    }
    return sum
}

func main() {
    fmt.Println("The sums of positive divisors for the first 100 positive integers are:")
    for i := 1; i <= 100; i++ {
        fmt.Printf("%3d   ", sumDivisors(i))
        if i%10 == 0 {
            fmt.Println()
        }
    }
}
Output:
The sums of positive divisors for the first 100 positive integers are:
  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   

GW-BASIC

5 PRINT 1,
10 FOR N = 2 TO 100
20 P = 1 + N
30 FOR I = 2 TO INT(N/2)
40 IF N MOD I = 0 THEN P = P + I
50 NEXT I
60 PRINT P,
70 NEXT N
Output:

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

Haskell

import Data.List.Split (chunksOf)

------------------------- DIVISORS -----------------------
divisors
  :: Integral a
  => a -> [a]
divisors n =
  ((<>) <*> (rest . reverse . fmap (quot n))) $
  filter ((0 ==) . rem n) [1 .. root]
  where
    root = (floor . sqrt . fromIntegral) n
    rest
      | n == root * root = tail
      | otherwise = id

-------------- SUMS AND PRODUCTS OF DIVISORS -------------
main :: IO ()
main =
  mapM_
    putStrLn
    [ "Sums of divisors of [1..100]:"
    , test sum
    , "Products of divisors of [1..100]:"
    , test product
    ]

test
  :: (Show a, Integral a)
  => ([a] -> a) -> String
test f =
  let xs = show . f . divisors <$> [1 .. 100]
      w = maximum $ length <$> xs
  in unlines $ unwords <$> fmap (fmap (justifyRight w ' ')) (chunksOf 5 xs)

justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)
Output:
Sums of divisors of [1..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

Products of divisors of [1..100]:
           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

J

Brute force:

   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

Of course, there are other alternatives.

Java

Translation of: C++
public class DivisorSum {
    private static long divisorSum(long n) {
        var total = 1L;
        var power = 2L;
        // Deal with powers of 2 first
        for (; (n & 1) == 0; power <<= 1, n >>= 1) {
            total += power;
        }
        // Odd prime factors up to the square root
        for (long p = 3; p * p <= n; p += 2) {
            long sum = 1;
            for (power = p; n % p == 0; power *= p, n /= p) {
                sum += power;
            }
            total *= sum;
        }
        // If n > 1 then it's prime
        if (n > 1) {
            total *= n + 1;
        }
        return total;
    }

    public static void main(String[] args) {
        final long limit = 100;
        System.out.printf("Sum of divisors for the first %d positive integers:%n", limit);
        for (long n = 1; n <= limit; ++n) {
            System.out.printf("%4d", divisorSum(n));
            if (n % 10 == 0) {
                System.out.println();
            }
        }
    }
}
Output:
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

jq

Works with: jq

Works with gojq, the Go implementation of jq

Since a "divisors" function is more likely to be generally useful than a "sum of divisors" function, this entry implements the latter in terms of the former.

# 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 add(s): reduce s as $x (null; .+$x);

def sum_of_divisors: add(divisors);

# For pretty-printing
def nwise($n):
  def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
  n;

def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;

# The task:
[range(1; 101) | sum_of_divisors] | nwise(10) | map(lpad(4)) | join("")
Output:
   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

Julia

using Primes

function sumdivisors(n)
    f = [one(n)]
    for (p, e) in factor(n)
        f = reduce(vcat, [f * p^j for j in 1:e], init = f)
    end
    return sum(f)
end

for i in 1:100
    print(rpad(sumdivisors(i), 5), i % 25 == 0 ? " \n" : "")
end
Output:
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

Kotlin

Translation of: C++
fun divisorSum(n: Long): Long {
    var nn = n
    var total = 1L
    var power = 2L
    // Deal with powers of 2 first
    while ((nn and 1) == 0L) {
        total += power

        power = power shl 1
        nn = nn shr 1
    }
    // Odd prime factors up to the square root
    var p = 3L
    while (p * p <= nn) {
        var sum = 1L
        power = p
        while (nn % p == 0L) {
            sum += power

            power *= p
            nn /= p
        }
        total *= sum

        p += 2
    }
    // If n > 1 then it's prime
    if (nn > 1) {
        total *= nn + 1
    }
    return total
}

fun main() {
    val limit = 100L
    println("Sum of divisors for the first $limit positive integers:")
    for (n in 1..limit) {
        print("%4d".format(divisorSum(n)))
        if (n % 10 == 0L) {
            println()
        }
    }
}
Output:
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

Lua

Translation of: C++

...via Algol 68

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
Output:
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

MAD

            NORMAL MODE IS INTEGER
            DIMENSION DIVSUM(100)
           
            THROUGH INIT, FOR I=1, 1, I.G.100
INIT        DIVSUM(I) = 1

            THROUGH CALC, FOR D=2, 1, D.G.100
            THROUGH CALC, FOR M=D, D, M.G.100
CALC        DIVSUM(M) = DIVSUM(M) + D

            THROUGH SHOW, FOR I=1, 10, I.G.100
SHOW        PRINT FORMAT F, DIVSUM(I), DIVSUM(I+1),
          0       DIVSUM(I+2), DIVSUM(I+3), DIVSUM(I+4),
          1       DIVSUM(I+5), DIVSUM(I+6), DIVSUM(I+7),
          2       DIVSUM(I+8), DIVSUM(I+9)

            VECTOR VALUES F = $10(I4)*$
            END OF PROGRAM
Output:
   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

Mathematica/Wolfram Language

DivisorSigma[1, Range[100]]
Output:
{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}

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(", ")
Output:
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

Nim

import math, strutils

proc divisors(n: Positive): seq[int] =
  for d in 1..sqrt(n.toFloat).int:
    if n mod d == 0:
      result.add d
      if n div d != d:
        result.add n div d

for n in 1..100:
  stdout.write ($sum(n.divisors)).align(3), if (n + 1) mod 10 == 0: '\n' else: ' '
Output:
  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

Pascal

Free Pascal

Brute force version.Checking all divisors up to sqrt(n). More clever is Sum_of_divisors#Delphi.But why not a different version.
Runs with gpc too.//cardinal is 32 or 64 bit depending on OS-System

program Sum_of_divisors;
{$IFDEF WINDOWS}}
  {$APPTYPE CONSOLE}
{$ENDIF}
{$IFDEF DELPHI}
uses
  System.SysUtils;
{$ENDIF}

function DivisorSum(n: Cardinal): Cardinal;
//check up to i*i= n
var
  i,quot,total: Cardinal;
begin
  total :=n+1;
  i := 2;
  repeat
    quot := n div i;
    //i >= sqrt(n) reached
    if quot <= i then
      BREAK;
    // n mod i = 0
    if quot*i = n then
      inc(total,i+quot);
    inc(i);
  until false;
  if i*i = n then
    inc(total,i);
  DivisorSum := total;
end;

const
  limit = 100;
var
  res,
  n :  cardinal;

begin
  writeln('Sum of divisors for the first ', limit, ' positive integers:');
  for  n := 1 to limit do
  begin
    res := divisorSum(n);
    Write(res: 4);
    if n mod 20 = 0 then
      writeln;
  end;
{$IFDEF WINDOWS}}
  readln;
{$ENDIF}
end.
Output:
Sum of divisors for the first 100 positive integers:
   2   5   4   7   6  15   8  15  13  18  12  32  14  24  24  31  18  39  20  47
  32  36  24  60  31  42  40  56  30  78  32  63  48  54  48  91  38  60  56  90
  42 103  44  84  78  72  48 124  57  93  72  98  54 120  72 128  80  90  60 168
  62  96 104 127  84 144  68 126  96 144  72 204  74 114 124 140  96 168  80 186
 121 126  84 224 108 132 120 180  90 244 112 168 128 144 120 252  98 171 156 217

Perl

Library: ntheory
use strict;
use warnings;
use feature 'say';
use ntheory 'divisor_sum';

my @x;
push @x, scalar divisor_sum($_) for 1..100;

say "Divisor sums - first 100:\n" .
    ((sprintf "@{['%4d' x 100]}", @x[0..100-1]) =~ s/(.{80})/$1\n/gr);
Output:
   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

PARI/GP

vector(100,X,sigma(X))
Output:

[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]

Phix

imperative

for i=1 to 100 do
    printf(1,"%4d",{sum(factors(i,1))})
    if remainder(i,10)=0 then puts(1,"\n") end if
end for
Output:
   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

functional

same output

sequence r = apply(apply(true,factors,{tagset(100),{1}}),sum)
puts(1,join_by(apply(true,sprintf,{{"%4d"},r}),1,10,""))

inline assembly

just to show it can be done, not that many would want to, same output

without javascript_semantics
for i=1 to 100 do
    integer res
    #ilASM {mov edi,[i]
            mov ecx,1
            xor esi,esi
            cmp edi,ecx
            je done
         ::add_divisor
            add esi,ecx
         ::next_divisor
            add ecx,1
            mov eax,edi
            xor edx,edx
            div ecx
            cmp eax,ecx
            jb done
            je square_root
            test edx,edx
            jnz next_divisor
            add esi,eax
            jmp add_divisor
         ::square_root
            test edx,edx
            jnz done
            add esi,eax
         ::done
            add esi,edi
            mov [res],esi
           }
    printf(1,"%4d",res)
    if remainder(i,10)=0 then puts(1,"\n") end if
end for

PicoLisp

(de propdiv (N)
   (let S 0
      (for X N
         (and (=0 (% N X)) (inc 'S X)) )
      S ) )
(do 10
   (do 10
      (prin (align 4 (propdiv (inc (0))))) )
      (prinl) )
Output:
   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

PL/M

100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;

DECLARE LIMIT LITERALLY '100';

PRINT$NUMBER: PROCEDURE (N);
    DECLARE S (7) BYTE INITIAL (' .....$');
    DECLARE N ADDRESS, I BYTE;
    I = 6;
DIGIT:
    I = I - 1;
    S(I) = N MOD 10 + '0';
    N = N/10;
    IF N>0 THEN GO TO DIGIT;
    I = I - 1;
    DO WHILE I > 0;
        S(I) = ' ';
        I = I - 1;
    END;
    CALL PRINT(.S);
END PRINT$NUMBER;

/* CALCULATE SUMS OF DIVISORS UP TO N INCLUSIVE */
CALC$DIVSUM: PROCEDURE (N, BUF);
    DECLARE (I, J, N, BUF, D BASED BUF) ADDRESS;
    DO I = 1 TO N;
        D(I) = 1;
    END;
    DO I = 2 TO N;
        DO J = I TO N BY I;
            D(J) = D(J) + I;
        END;
    END;
END CALC$DIVSUM;

/* PRINT RESULTS */
DECLARE I ADDRESS;
DECLARE COL BYTE INITIAL (0);
DECLARE DIVS (LIMIT) ADDRESS;

CALL CALC$DIVSUM(LIMIT, .DIVS);
DO I = 1 TO LIMIT;
    CALL PRINT$NUMBER(DIVS(I));
    COL = COL + 1;
    IF COL = 10 THEN DO;
        CALL PRINT(.(13,10,'$'));
        COL = 0;
    END;
END;
CALL EXIT;
EOF
Output:
     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

Python

Using prime factorization

def factorize(n):
    assert(isinstance(n, int))
    if n < 0: 
        n = -n 
    if n < 2: 
        return 
    k = 0 
    while 0 == n%2: 
        k += 1 
        n //= 2 
    if 0 < k: 
        yield (2,k) 
    p = 3 
    while p*p <= n: 
        k = 0 
        while 0 == n%p: 
            k += 1 
            n //= p 
        if 0 < k: 
            yield (p,k)
        p += 2 
    if 1 < n: 
        yield (n,1) 

def sum_of_divisors(n): 
    assert(n != 0) 
    ans = 1 
    for (p,k) in factorize(n): 
        ans *= (pow(p,k+1) - 1)//(p-1) 
    return ans 
    
if __name__ == "__main__":
    print([sum_of_divisors(n) for n in range(1,101)])

Finding divisors efficiently

def sum_of_divisors(n):
    assert(isinstance(n, int) and 0 < n)
    ans, i, j = 0, 1, 1
    while i*i <= n:
        if 0 == n%i:
            ans += i
            j = n//i
            if j != i:
                ans += j
        i += 1
    return ans
    
if __name__ == "__main__":
    print([sum_of_divisors(n) for n in range(1,101)])
Output:
[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]

Choosing the right abstraction

This is really an exercise in defining a divisors function, and the only difference between the suggested Sum and Product draft tasks boils down to two trivial morphemes:

reduce(add, divisors(n), 0) vs reduce(mul, divisors(n), 1)

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.

'''Sums and products of divisors'''

from math import floor, sqrt
from functools import reduce
from operator import add, mul


# divisors :: Int -> [Int]
def divisors(n):
    '''List of all divisors of n including n itself.
    '''
    root = floor(sqrt(n))
    lows = [x for x in range(1, 1 + root) if 0 == n % x]
    return lows + [n // x for x in reversed(lows)][
        (1 if n == (root * root) else 0):
    ]


# ------------------------- TEST -------------------------
# main :: IO ()
def main():
    '''Sums and products of divisors for each integer in range [1..50]
    '''
    print('Products of divisors:')
    for n in range(1, 1 + 50):
        print(n, '->', reduce(mul, divisors(n), 1))

    print('Sums of divisors:')
    for n in range(1, 1 + 100):
        print(n, '->', reduce(add, divisors(n), 0))


# MAIN ---
if __name__ == '__main__':
    main()

Quackery

factors is defined at Factors of an integer#Quackery.

  [ 0 swap factors witheach + ] is sum-of-divisors 

  [] []
  100 times
    [ i^ 1+ sum-of-divisors join ]
  witheach [ number$ nested join ]
  72 wrap$
Output:
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

R

This only takes one line.

sapply(1:100, function(n) sum(c(Filter(function(x) n %% x == 0, seq_len(n %/% 2)), n)))

Racket

#lang racket/base

(require math/number-theory)

(define (sum-of-divisors n) (apply + (divisors n)))

(displayln (for/list ((n (in-range 1 101))) (sum-of-divisors n)))
Output:
(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)

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.

use Prime::Factor:ver<0.3.0+>;
use Lingua::EN::Numbers;

say "\nTau function - first 100:\n",        # ID
(1..*).map({ +.&divisors })[^100]\          # the task
.batch(20)».fmt("%3d").join("\n");          # display formatting

say "\nTau numbers - first 100:\n",         # ID
(1..*).grep({ $_ %% +.&divisors })[^100]\   # the task
.batch(10)».&comma».fmt("%5s").join("\n");  # display formatting

say "\nDivisor sums - first 100:\n",        # ID
(1..*).map({ [+] .&divisors })[^100]\       # the task
.batch(20)».fmt("%4d").join("\n");          # display formatting

say "\nDivisor products - first 100:\n",    # ID
(1..*).map({ [×] .&divisors })[^100]\       # the task
.batch(5)».&comma».fmt("%16s").join("\n");  # display formatting
Output:
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

REXX

/*REXX program displays the first   N   sum of divisors  (shown in a columnar format).  */
parse arg n cols .                               /*obtain optional argument from the CL.*/
if    n=='' |    n==","  then    n= 100          /*Not specified?  Then use the default.*/
if cols=='' | cols==","  then cols=  10          /* "      "         "   "   "     "    */
say ' index │'center("sum of divisors", 102)     /*display the title for the column #s. */
say '───────┼'center(""               , 102,'─') /*   "     "  separator for the title. */
w= 10                                            /*W:  used to align 1st output column. */
$=;                            idx= 1            /*$:  the output list, shown in columns*/
       do j=1  for N                             /*process  N  positive integers.       */
       $= $  ||  right( commas( sigma(j) ), w)   /*add a sigma (sum) to the output list.*/
       if j//cols\==0  then iterate              /*Not a multiple of cols? Don't display*/
       say center(idx, 7)'│'            $        /*display partial list to the terminal.*/
       idx= idx + cols                           /*bump the index number for the output.*/
       $=                                        /*start with a blank line for next line*/
       end   /*j*/

if $\==''  then say center(idx, 7)'│'   $        /*any residuals sums left to display?  */
say '───────┴'center(""               , 102,'─') /*   "     "  foot separator for data. */
exit 0                                           /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?;  do jc=length(?)-3  to 1  by -3; ?=insert(',', ?, jc); end;  return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
sigma: procedure; parse arg x; if x==1 then return 1;  odd=x // 2    /* // ◄──remainder.*/
       s= 1 + x                                  /* [↓]  only use  EVEN or ODD integers.*/
             do k=2+odd  by 1+odd  while k*k<x   /*divide by all integers up to  √x.    */
             if x//k==0  then  s= s + k +  x % k /*add the two divisors to (sigma) sum. */
             end   /*k*/                         /* [↑]  %  is the REXX integer division*/
       if k*k==x  then  return s + k             /*Was  X  a square?   If so, add  √ x  */
                        return s                 /*return (sigma) sum of the divisors.  */
output   when using the default input:
 index │                                           sum of divisors
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │          1         3         4         7         6        12         8        15        13        18
  11   │         12        28        14        24        24        31        18        39        20        42
  21   │         32        36        24        60        31        42        40        56        30        72
  31   │         32        63        48        54        48        91        38        60        56        90
  41   │         42        96        44        84        78        72        48       124        57        93
  51   │         72        98        54       120        72       120        80        90        60       168
  61   │         62        96       104       127        84       144        68       126        96       144
  71   │         72       195        74       114       124       140        96       168        80       186
  81   │        121       126        84       224       108       132       120       180        90       234
  91   │        112       168       128       144       120       252        98       171       156       217
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────

Ring

see "the sums of divisors for  100  integers:" + nl
num = 0

for n = 1 to 100
    sum = 0
    for m = 1 to n
        if n%m = 0
           sum = sum + m
        ok
    next
    num = num + 1
    if num%10 = 1
       see nl
    ok
    see "" + sum + " "
next

Output:

the sums of divisors for  100  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 

RPL

Translation of: Python
Works with: Halcyon Calc version 4.2.8
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
Input:
≪ { } 1 100 FOR j j ∑DIV + NEXT ≫ EVAL
Output:
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 }

Ruby

Translation of: C++
def divisor_sum(n)
    total = 1
    power = 2
    # Deal with powers of 2 first
    while (n & 1) == 0
        total = total + power

        power = power << 1
        n = n >> 1
    end
    # Odd prime factors up to the square root
    p = 3
    while p * p <= n
        sum = 1

        power = p
        while n % p == 0
            sum = sum + power

            power = power * p
            n = (n / p).floor
        end
        total = total * sum

        p = p + 2
    end
    # If n > 1 then it's prime
    if n > 1 then
        total = total * (n + 1)
    end
    return total
end

LIMIT = 100
print "Sum of divisors for the first ", LIMIT, " positive integers:\n"
for n in 1 .. LIMIT
    print "%4d" % [divisor_sum(n)]
    if n % 10 == 0 then
        print "\n"
    end
end
Output:
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

Sidef

1..100 -> map { .sigma }.say
Output:
[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]

Tiny BASIC

    PRINT 1
    LET N = 1
 10 LET N = N + 1
    LET S = 1 + N
    LET I = 1
 20 LET I = I + 1
    IF I > N/2 THEN GOTO 30
    IF (N/I)*I = N THEN LET S = S + I
    GOTO 20
 30 PRINT S
    IF N < 100 THEN GOTO 10
    END

Verilog

module main;
  integer n, p, i;
  
  initial begin
      $write("1");
      for(n=2; n<=100; n=n+1) begin
    	p = 1 + n;
    	for(i=2; i<=n/2; i=i+1) if(n % i == 0) p = p + i;
    	$write(p);
      end
      $finish ;
    end
endmodule

VTL-2

10 C=0
20 M=100
30 I=1
40 :I)=0
50 I=I+1
60 #=M>I*40
70 I=1
80 J=I
90 :J)=:J)+I
100 J=J+I
110 #=M>J*90
120 ?=:I)
130 $=9
140 C=C+1
150 #=C/10*0+0<%*170
160 ?=""
170 I=I+1
180 #=M>I*80
Output:
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

Wren

Library: Wren-math
Library: Wren-fmt
import "./math" for Int, Nums
import "./fmt" for Fmt

System.print("The sums of positive divisors for the first 100 positive integers are:")
for (i in 1..100) {
    Fmt.write("$3d   ", Nums.sum(Int.divisors(i)))
    if (i % 10 == 0) System.print()
}
Output:
The sums of positive divisors for the first 100 positive integers are:
  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   

XPL0

func SumDiv(N);         \Return sum of divisors of N
int  N, Sum, Div;
[Sum:= 0;
for Div:= 1 to N do
    if rem(N/Div) = 0 then
        Sum:= Sum + Div;
return Sum;
];

int C, N;
[C:= 0;
for N:= 1 to 100 do
    [IntOut(0, SumDiv(N));
    C:= C+1;
    if rem(C/10) then ChOut(0, 9\tab\) else CrLf(0)];
]
Output:
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