Sum of divisors: Difference between revisions

Added Easylang
(Add Comal)
(Added Easylang)
 
(16 intermediate revisions by 11 users not shown)
Line 12:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F sum_of_divisors(n)
V ans = 0
V i = 1
Line 25:
R ans
 
print((1..100).map(n -> sum_of_divisors(n)))</langsyntaxhighlight>
 
{{out}}
Line 33:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC PrintNum(BYTE x)
Put(32)
IF x<10 THEN Put(32) FI
Line 64:
 
LMARGIN=oldLMARGIN ;restore left margin on the screen
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_divisors.png Screenshot from Atari 8-bit computer]
Line 78:
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|C++}}...via Algol W
<syntaxhighlight lang="algol68">
BEGIN # sum the divisors of the first 100 positive integers #
 
# computes the sum of the divisors of v using the prime factorisation #
PROC divisor sum = ( INT v )INT:
BEGIN
INT total := 1, power := 2, n := v;
WHILE NOT ODD n DO # Deal with powers of 2 first #
total +:= power;
power *:= 2;
n OVERAB 2
OD;
INT p := 3; # Odd prime factors up to the square root #
WHILE ( p * p ) <= n DO
INT sum := 1;
power := p;
WHILE n MOD p = 0 DO
sum +:= power;
power *:= p;
n OVERAB p
OD;
p +:= 2;
total *:= sum
OD;
IF n > 1 THEN total *:= n + 1 FI; # If n > 1 then it's prime #
total
END # divisor sum # ;
BEGIN # show the first 100 divisor sums #
INT limit = 100;
print( ( "Sum of divisors for the first ", whole( limit, 0 ), " positive integers:" ) );
FOR n TO limit DO
IF n MOD 10 = 1 THEN print( ( newline ) ) FI;
print( ( " ", whole( divisor sum( n ), -4 ) ) )
OD
END
END
</syntaxhighlight>
{{out}}
<pre>
Sum of divisors for the first 100 positive integers:
1 3 4 7 6 12 8 15 13 18
12 28 14 24 24 31 18 39 20 42
32 36 24 60 31 42 40 56 30 72
32 63 48 54 48 91 38 60 56 90
42 96 44 84 78 72 48 124 57 93
72 98 54 120 72 120 80 90 60 168
62 96 104 127 84 144 68 126 96 144
72 195 74 114 124 140 96 168 80 186
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
</pre>
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algolm">begin
integer N;
N := 100;
Line 101 ⟶ 155:
end;
end;
end</langsyntaxhighlight>
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 116 ⟶ 170:
=={{header|ALGOL W}}==
{{Trans|C++}}
<langsyntaxhighlight lang="algolw">begin % sum the divisors of the first 100 positive integers %
% computes the sum of the divisors of n using the prime %
% factorisation %
Line 155 ⟶ 209:
end for_n
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 173 ⟶ 227:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">10 10 ⍴ +/∘(⍸0=⍳|⊢)¨⍳100</langsyntaxhighlight>
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 188 ⟶ 242:
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">on sumOfDivisors(n)
if (n < 1) then return 0
set sum to 0
Line 222 ⟶ 276:
end task
 
return task()</langsyntaxhighlight>
 
{{output}}
Line 230 ⟶ 284:
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|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}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUM_OF_DIVISORS.AWK
# converted from Go
Line 262 ⟶ 335:
return(ans)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 279 ⟶ 352:
 
=={{header|BASIC}}==
<langsyntaxhighlight BASIClang="basic">10 DEFINT A-Z: DATA 100
20 READ M
30 DIM D(M)
Line 285 ⟶ 358:
50 FOR J=I TO M STEP I: D(J)=D(J)+I: NEXT
60 PRINT D(I),
70 NEXT</langsyntaxhighlight>
{{out}}
<pre> 1 3 4 7 6
Line 310 ⟶ 383:
 
==={{header|BASIC256}}===
<langsyntaxhighlight lang="freebasic">print 1; chr(9);
for n = 2 to 100
p = 1 + n
Line 318 ⟶ 391:
print p; chr(9);
next n
end</langsyntaxhighlight>
 
==={{header|PureBasic}}===
<langsyntaxhighlight PureBasiclang="purebasic">OpenConsole()
Print("1")
For n.i = 2 To 100
Line 331 ⟶ 404:
Next n
Input()
CloseConsole()</langsyntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<langsyntaxhighlight QBasiclang="qbasic">PRINT 1,
FOR n = 2 TO 100
p = 1 + n
Line 344 ⟶ 417:
PRINT p,
NEXT n
END</langsyntaxhighlight>
 
==={{header|True BASIC}}===
<langsyntaxhighlight lang="qbasic">PRINT 1,
FOR n = 2 To 100
LET p = 1 + n
Line 355 ⟶ 428:
PRINT p,
NEXT n
END</langsyntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="freebasic">print 1,
for n = 2 to 100
p = 1 + n
Line 366 ⟶ 439:
print p,
next n
end</langsyntaxhighlight>
 
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
manifest $( MAXIMUM = 100 $)
Line 398 ⟶ 471:
if col rem 10 = 0 then wrch('*N')
$)
$)</langsyntaxhighlight>
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 413 ⟶ 486:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight Clang="c">#include <stdio.h>
 
// See https://en.wikipedia.org/wiki/Divisor_function
Line 449 ⟶ 522:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
Line 464 ⟶ 537:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 494 ⟶ 567:
std::cout << '\n';
}
}</langsyntaxhighlight>
 
{{out}}
Line 510 ⟶ 583:
112 168 128 144 120 252 98 171 156 217
</pre>
 
=={{header|Clojure}}==
{{trans|Raku}}
<syntaxhighlight lang="clojure>(require '[clojure.string :refer [join]])
(require '[clojure.pprint :refer [cl-format]])
 
(defn divisors [n] (filter #(zero? (rem n %)) (range 1 (inc n))))
 
(defn display-results [label per-line width nums]
(doall (map println (cons (str "\n" label ":") (list
(join "\n" (map #(join " " %)
(partition-all per-line
(map #(cl-format nil "~v:d" width %) nums)))))))))
 
(display-results "Tau function - first 100" 20 3
(take 100 (map (comp count divisors) (drop 1 (range)))))
 
(display-results "Tau numbers – first 100" 10 5
(take 100 (filter #(zero? (rem % (count (divisors %)))) (drop 1 (range)))))
 
(display-results "Divisor sums – first 100" 20 4
(take 100 (map #(reduce + (divisors %)) (drop 1 (range)))))
 
(display-results "Divisor products – first 100" 5 16
(take 100 (map #(reduce * (divisors %)) (drop 1 (range)))))
</syntaxhighlight>
{{Out}}
<pre>Tau function - first 100:
1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
4 4 2 8 3 4 4 6 2 8 2 6 4 4 4 9 2 4 4 8
2 8 2 6 6 4 2 10 3 6 4 6 2 8 4 8 4 4 2 12
2 4 6 7 4 8 2 6 4 8 2 12 2 4 6 6 4 8 2 10
5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
 
Tau numbers – first 100:
1 2 8 9 12 18 24 36 40 56
60 72 80 84 88 96 104 108 128 132
136 152 156 180 184 204 225 228 232 240
248 252 276 288 296 328 344 348 360 372
376 384 396 424 441 444 448 450 468 472
480 488 492 504 516 536 560 564 568 584
600 612 625 632 636 640 664 672 684 708
712 720 732 776 792 804 808 824 828 852
856 864 872 876 880 882 896 904 936 948
972 996 1,016 1,040 1,044 1,048 1,056 1,068 1,089 1,096
 
Divisor sums – first 100:
1 3 4 7 6 12 8 15 13 18 12 28 14 24 24 31 18 39 20 42
32 36 24 60 31 42 40 56 30 72 32 63 48 54 48 91 38 60 56 90
42 96 44 84 78 72 48 124 57 93 72 98 54 120 72 120 80 90 60 168
62 96 104 127 84 144 68 126 96 144 72 195 74 114 124 140 96 168 80 186
121 126 84 224 108 132 120 180 90 234 112 168 128 144 120 252 98 171 156 217
 
Divisor products – first 100:
1 2 3 8 5
36 7 64 27 100
11 1,728 13 196 225
1,024 17 5,832 19 8,000
441 484 23 331,776 125
676 729 21,952 29 810,000
31 32,768 1,089 1,156 1,225
10,077,696 37 1,444 1,521 2,560,000
41 3,111,696 43 85,184 91,125
2,116 47 254,803,968 343 125,000
2,601 140,608 53 8,503,056 3,025
9,834,496 3,249 3,364 59 46,656,000,000
61 3,844 250,047 2,097,152 4,225
18,974,736 67 314,432 4,761 24,010,000
71 139,314,069,504 73 5,476 421,875
438,976 5,929 37,015,056 79 3,276,800,000
59,049 6,724 83 351,298,031,616 7,225
7,396 7,569 59,969,536 89 531,441,000,000
8,281 778,688 8,649 8,836 9,025
782,757,789,696 97 941,192 970,299 1,000,000,000</pre>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="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
Line 535 ⟶ 682:
if col // 10 = 0 then stream$putc(po, '\n') end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 549 ⟶ 696:
 
=={{header|Comal}}==
<langsyntaxhighlight lang="comal">0010 max#:=100
0020 //
0030 DIM divsum#(max#)
Line 560 ⟶ 707:
0100 IF i# MOD 10=0 THEN PRINT
0110 ENDFOR i#
0120 END</langsyntaxhighlight>
{{out}}
<pre>1 3 4 7 6 12 8 15 13 18
Line 574 ⟶ 721:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
<lang Lisp>
(format t "~{~a ~}~%"
(loop for a from 1 to 100 collect
Line 580 ⟶ 727:
when (zerop (rem a b))
sum b)))
</syntaxhighlight>
</lang>
{{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|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
const MAXIMUM := 100;
Line 628 ⟶ 775:
end if;
i := i + 1;
end loop;</langsyntaxhighlight>
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 643 ⟶ 790:
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio;
 
// See https://en.wikipedia.org/wiki/Divisor_function
Line 676 ⟶ 823:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
Line 693 ⟶ 840:
{{libheader| System.SysUtils}}
{{Trans|Java}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Sum_of_divisors;
 
Line 750 ⟶ 897:
 
{$IFNDEF UNIX} readln; {$ENDIF}
end.</langsyntaxhighlight>
 
=={{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#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
<langsyntaxhighlight lang="fsharp">
// Sum of divisors. Nigel Galloway: March 9th., 2021
let sod u=let P=primes32()
Line 761 ⟶ 923:
let n=Seq.head P in fG 1 n 1 1 n
[1..100]|>Seq.iter(sod>>printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 769 ⟶ 931:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">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.</langsyntaxhighlight>
{{out}}
<pre>
Line 790 ⟶ 952:
 
=={{header|Fermat}}==
<langsyntaxhighlight 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</langsyntaxhighlight>
 
=={{header|FOCAL}}==
<langsyntaxhighlight FOCALlang="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
Line 804 ⟶ 966:
02.30 I (9-C)2.4;R
02.40 T !
02.50 S C=0</langsyntaxhighlight>
{{out}}
<pre>= 1= 3= 4= 7= 6= 12= 8= 15= 13= 18
Line 819 ⟶ 981:
=={{header|Forth}}==
{{trans|C++}}
<langsyntaxhighlight lang="forth">: divisor_sum ( n -- n )
1 >r
2
Line 859 ⟶ 1,021:
 
100 print_divisor_sums
bye</langsyntaxhighlight>
 
{{out}}
Line 877 ⟶ 1,039:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran"> program DivSum
implicit none
integer i, j, col, divs(100)
Line 897 ⟶ 1,059:
end if
30 continue
end program </langsyntaxhighlight>
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 911 ⟶ 1,073:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">dim p as ulongint
print 1,
for n as uinteger = 2 to 100
Line 919 ⟶ 1,081:
next i
print p,
next n</langsyntaxhighlight>
{{out}}
<pre> 1 3 4 7 6 12
Line 938 ⟶ 1,100:
112 168 128 144 120 252
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æ}}==
 
{{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}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 980 ⟶ 1,162:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 998 ⟶ 1,180:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">
5 PRINT 1,
10 FOR N = 2 TO 100
Line 1,006 ⟶ 1,188:
50 NEXT I
60 PRINT P,
70 NEXT N</langsyntaxhighlight>
{{out}}<pre>
1 3 4 7 6 12 8 15 13 18 12
Line 1,020 ⟶ 1,202:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List.Split (chunksOf)
 
------------------------- DIVISORS -----------------------
Line 1,055 ⟶ 1,237:
 
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</langsyntaxhighlight>
{{Out}}
<pre>Sums of divisors of [1..100]:
Line 1,100 ⟶ 1,282:
8281 778688 8649 8836 9025
782757789696 97 941192 970299 1000000000</pre>
 
=={{header|J}}==
Brute force:
<syntaxhighlight lang=J> spd=: {{+/I.0=y|~i.1+y}}"0
spd 1+i.10 10
1 3 4 7 6 12 8 15 13 18
12 28 14 24 24 31 18 39 20 42
32 36 24 60 31 42 40 56 30 72
32 63 48 54 48 91 38 60 56 90
42 96 44 84 78 72 48 124 57 93
72 98 54 120 72 120 80 90 60 168
62 96 104 127 84 144 68 126 96 144
72 195 74 114 124 140 96 168 80 186
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
</syntaxhighlight>
 
Of course, there are [[j:Essays/Divisors#Sum_of_Divisors|other alternatives]].
 
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">public class DivisorSum {
private static long divisorSum(long n) {
var total = 1L;
Line 1,136 ⟶ 1,336:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
Line 1,156 ⟶ 1,356:
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.
<syntaxhighlight lang="jq">
<lang jq>
# divisors as an unsorted stream
def divisors:
Line 1,184 ⟶ 1,384:
 
# The task:
[range(1; 101) | sum_of_divisors] | nwise(10) | map(lpad(4)) | join("")</langsyntaxhighlight>
{{out}}
<pre>
Line 1,200 ⟶ 1,400:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function sumdivisors(n)
Line 1,213 ⟶ 1,413:
print(rpad(sumdivisors(i), 5), i % 25 == 0 ? " \n" : "")
end
</langsyntaxhighlight>{{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
Line 1,223 ⟶ 1,423:
=={{header|Kotlin}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">fun divisorSum(n: Long): Long {
var nn = n
var total = 1L
Line 1,265 ⟶ 1,465:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
Line 1,278 ⟶ 1,478:
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217</pre>
 
=={{header|Lua}}==
{{Trans|C++}}...via Algol 68
<syntaxhighlight lang="lua">
do -- sum the divisors of the first 100 positive integers
 
-- computes the sum of the divisors of v using the prime factorisation
function divisor_sum( v )
local total, power, n = 1, 2, v
while n % 2 == 0 do -- Deal with powers of 2 first
total = total + power
power = power * 2
n = math.floor( n / 2 )
end
local p = 3 -- Odd prime factors up to the square root
while ( p * p ) <= n do
local sum = 1
power = p
while n % p == 0 do
sum = sum + power
power = power * p
n = math.floor( n / p )
end
p = p + 2
total = total * sum
end
if n > 1 then total = total * ( n + 1 ) end -- If n > 1 then it's prime
return total
end
 
-- show the first 100 divisor sums
local limit = 100
io.write( "Sum of divisors for the first ", limit, " positive integers:\n" )
for n = 1, limit do
io.write( string.format( " %4d", divisor_sum( n ) ) )
if n % 10 == 0 then io.write( "\n" ) end
end
 
end
</syntaxhighlight>
{{out}}
<pre>
Sum of divisors for the first 100 positive integers:
1 3 4 7 6 12 8 15 13 18
12 28 14 24 24 31 18 39 20 42
32 36 24 60 31 42 40 56 30 72
32 63 48 54 48 91 38 60 56 90
42 96 44 84 78 72 48 124 57 93
72 98 54 120 72 120 80 90 60 168
62 96 104 127 84 144 68 126 96 144
72 195 74 114 124 140 96 168 80 186
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
</pre>
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
DIMENSION DIVSUM(100)
Line 1,297 ⟶ 1,551:
 
VECTOR VALUES F = $10(I4)*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 1,311 ⟶ 1,565:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">DivisorSigma[1, Range[100]]</langsyntaxhighlight>
{{out}}
<pre>{1, 3, 4, 7, 6, 12, 8, 15, 13, 18, 12, 28, 14, 24, 24, 31, 18, 39, 20, 42, 32, 36, 24, 60, 31, 42, 40, 56, 30, 72, 32, 63, 48, 54, 48, 91, 38, 60, 56, 90, 42, 96, 44, 84, 78, 72, 48, 124, 57, 93, 72, 98, 54, 120, 72, 120, 80, 90, 60, 168, 62, 96, 104, 127, 84, 144, 68, 126, 96, 144, 72, 195, 74, 114, 124, 140, 96, 168, 80, 186, 121, 126, 84, 224, 108, 132, 120, 180, 90, 234, 112, 168, 128, 144, 120, 252, 98, 171, 156, 217}</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
divisorSum = function(n)
ans = 0
i = 1
while i * i <= n
if n % i == 0 then
ans += i
j = floor(n / i)
if j != i then ans += j
end if
i += 1
end while
return ans
end function
 
sums = []
for n in range(1, 100)
sums.push(divisorSum(n))
end for
 
print sums.join(", ")
</syntaxhighlight>
{{out}}
<pre>
1, 3, 4, 7, 6, 12, 8, 15, 13, 18, 12, 28, 14, 24, 24, 31, 18, 39, 20, 42, 32, 36, 24, 60, 31, 42, 40, 56, 30, 72, 32, 63, 48, 54, 48, 91, 38, 60, 56, 90, 42, 96, 44, 84, 78, 72, 48, 124, 57, 93, 72, 98, 54, 120, 72, 120, 80, 90, 60, 168, 62, 96, 104, 127, 84, 144, 68, 126, 96, 144, 72, 195, 74, 114, 124, 140, 96, 168, 80, 186, 121, 126, 84, 224, 108, 132, 120, 180, 90, 234, 112, 168, 128, 144, 120, 252, 98, 171, 156, 217
</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
proc divisors(n: Positive): seq[int] =
Line 1,326 ⟶ 1,608:
 
for n in 1..100:
stdout.write ($sum(n.divisors)).align(3), if (n + 1) mod 10 == 0: '\n' else: ' '</langsyntaxhighlight>
 
{{out}}
Line 1,344 ⟶ 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>
Runs with gpc too.//cardinal is 32 or 64 bit depending on OS-System
<langsyntaxhighlight lang="pascal">
program Sum_of_divisors;
{$IFDEF WINDOWS}}
Line 1,394 ⟶ 1,676:
readln;
{$ENDIF}
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,406 ⟶ 1,688:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,415 ⟶ 1,697:
 
say "Divisor sums - first 100:\n" .
((sprintf "@{['%4d' x 100]}", @x[0..100-1]) =~ s/(.{80})/$1\n/gr);</langsyntaxhighlight>
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18 12 28 14 24 24 31 18 39 20 42
Line 1,424 ⟶ 1,706:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">vector(100,X,sigma(X))</langsyntaxhighlight>
{{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>
Line 1,430 ⟶ 1,712:
=={{header|Phix}}==
=== imperative ===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,451 ⟶ 1,733:
=== functional ===
same output
<!--<langsyntaxhighlight Phixlang="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: #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>
<!--</langsyntaxhighlight>-->
=== inline assembly ===
just to show it can be done, not that many would want to, same output
<!--<langsyntaxhighlight Phixlang="phix">-->
<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>
Line 1,491 ⟶ 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;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de propdiv (N)
(let S 0
(for X N
Line 1,502 ⟶ 1,784:
(do 10
(prin (align 4 (propdiv (inc (0))))) )
(prinl) )</langsyntaxhighlight>
{{out}}
<pre>
Line 1,518 ⟶ 1,800:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 1,570 ⟶ 1,852:
END;
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 1,585 ⟶ 1,867:
=={{header|Python}}==
===Using prime factorization===
<langsyntaxhighlight Pythonlang="python">def factorize(n):
assert(isinstance(n, int))
if n < 0:
Line 1,617 ⟶ 1,899:
if __name__ == "__main__":
print([sum_of_divisors(n) for n in range(1,101)])</langsyntaxhighlight>
 
===Finding divisors efficiently===
<langsyntaxhighlight Pythonlang="python">def sum_of_divisors(n):
assert(isinstance(n, int) and 0 < n)
ans, i, j = 0, 1, 1
Line 1,633 ⟶ 1,915:
if __name__ == "__main__":
print([sum_of_divisors(n) for n in range(1,101)])</langsyntaxhighlight>
{{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>
Line 1,647 ⟶ 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.
 
<langsyntaxhighlight lang="python">'''Sums and products of divisors'''
 
from math import floor, sqrt
Line 1,681 ⟶ 1,963:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
 
=={{header|Quackery}}==
Line 1,687 ⟶ 1,969:
<code>factors</code> is defined at [[Factors of an integer#Quackery]].
 
<langsyntaxhighlight Quackerylang="quackery"> [ 0 swap factors witheach + ] is sum-of-divisors
 
[] []
Line 1,693 ⟶ 1,975:
[ i^ 1+ sum-of-divisors join ]
witheach [ number$ nested join ]
72 wrap$ </langsyntaxhighlight>
 
{{out}}
Line 1,706 ⟶ 1,988:
=={{header|R}}==
This only takes one line.
<langsyntaxhighlight lang="rsplus">sapply(1:100, function(n) sum(c(Filter(function(x) n %% x == 0, seq_len(n %/% 2)), n)))</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket/base
 
(require math/number-theory)
Line 1,716 ⟶ 1,998:
(define (sum-of-divisors n) (apply + (divisors n)))
 
(displayln (for/list ((n (in-range 1 101))) (sum-of-divisors n)))</langsyntaxhighlight>
 
{{out}}
Line 1,725 ⟶ 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.
 
<syntaxhighlight lang="raku" perl6line>use Prime::Factor:ver<0.3.0+>;
use Lingua::EN::Numbers;
 
Line 1,742 ⟶ 2,024:
say "\nDivisor products - first 100:\n", # ID
(1..*).map({ [×] .&divisors })[^100]\ # the task
.batch(5)».&comma».fmt("%16s").join("\n"); # display formatting</langsyntaxhighlight>
{{out}}
<pre>Tau function - first 100:
Line 1,793 ⟶ 2,075:
 
=={{header|REXX}}==
<langsyntaxhighlight 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.*/
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/
Line 1,821 ⟶ 2,103:
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. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,840 ⟶ 2,122:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "the sums of divisors for 100 integers:" + nl
num = 0
Line 1,857 ⟶ 2,139:
see "" + sum + " "
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,872 ⟶ 2,154:
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
</pre>
 
=={{header|RPL}}==
{{trans|Python}}
{{works with|Halcyon Calc|4.2.8}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ → n
≪ 0
1 n √ '''FOR''' ii
'''IF''' n ii MOD NOT '''THEN'''
ii +
n ii / FLOOR
'''IF''' DUP ii ≠
'''THEN''' + '''ELSE''' DROP '''END'''
'''END NEXT'''
≫ ≫ ''''∑DIV'''' STO
|
'''∑DIV''' ''( n -- sum_of_divisors )''
ans = 0
while i*i <= n:
if 0 == n%i:
ans += i
j = n//i
if j != i:
ans += j
i += 1
return ans
|}
{{in}}
<pre>
≪ { } 1 100 FOR j j ∑DIV + NEXT ≫ EVAL
</pre>
{{out}}
<pre>
1: { 1 3 4 7 6 12 8 15 13 18 12 28 14 24 24 31 18 39 20 42 32 36 24 60 31 42 40 56 30 72 32 63 48 54 48 91 38 60 56 90 42 96 44 84 78 72 48 124 57 93 72 98 54 120 72 120 80 90 60 168 62 96 104 127 84 144 68 126 96 144 72 195 74 114 124 140 96 168 80 186 121 126 84 224 108 132 120 180 90 234 112 168 128 144 120 252 98 171 156 217 }
</pre>
 
=={{header|Ruby}}==
{{trans|C++}}
<langsyntaxhighlight lang="ruby">def divisor_sum(n)
total = 1
power = 2
Line 1,916 ⟶ 2,237:
print "\n"
end
end</langsyntaxhighlight>
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
Line 1,931 ⟶ 2,252:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">1..100 -> map { .sigma }.say</langsyntaxhighlight>
{{out}}
<pre>
Line 1,938 ⟶ 2,259:
 
=={{header|Tiny BASIC}}==
<langsyntaxhighlight lang="tinybasic">
PRINT 1
LET N = 1
Line 1,950 ⟶ 2,271:
30 PRINT S
IF N < 100 THEN GOTO 10
END</langsyntaxhighlight>
 
=={{header|Verilog}}==
<langsyntaxhighlight Veriloglang="verilog">module main;
integer n, p, i;
Line 1,965 ⟶ 2,286:
$finish ;
end
endmodule</langsyntaxhighlight>
 
=={{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}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int, Nums
import "./fmt" for Fmt
 
System.print("The sums of positive divisors for the first 100 positive integers are:")
Line 1,977 ⟶ 2,329:
Fmt.write("$3d ", Nums.sum(Int.divisors(i)))
if (i % 10 == 0) System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 1,995 ⟶ 2,347:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func SumDiv(N); \Return sum of divisors of N
int N, Sum, Div;
[Sum:= 0;
Line 2,010 ⟶ 2,362:
C:= C+1;
if rem(C/10) then ChOut(0, 9\tab\) else CrLf(0)];
]</langsyntaxhighlight>
 
{{out}}
2,016

edits