Sum of divisors: Difference between revisions
m
syntax highlighting fixup automation
(Frink) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 12:
{{trans|Python}}
<
V ans = 0
V i = 1
Line 25:
R ans
print((1..100).map(n -> sum_of_divisors(n)))</
{{out}}
Line 33:
=={{header|Action!}}==
<
Put(32)
IF x<10 THEN Put(32) FI
Line 64:
LMARGIN=oldLMARGIN ;restore left margin on the screen
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_divisors.png Screenshot from Atari 8-bit computer]
Line 81:
=={{header|ALGOL-M}}==
<
integer N;
N := 100;
Line 101:
end;
end;
end</
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 116:
=={{header|ALGOL W}}==
{{Trans|C++}}
<
% computes the sum of the divisors of n using the prime %
% factorisation %
Line 155:
end for_n
end
end.</
{{out}}
<pre>
Line 173:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 188:
=={{header|AppleScript}}==
<
if (n < 1) then return 0
set sum to 0
Line 222:
end task
return task()</
{{output}}
Line 233:
=={{header|Arturo}}==
<
print map row 'r -> pad to :string r 4
]</
{{out}}
Line 251:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUM_OF_DIVISORS.AWK
# converted from Go
Line 281:
return(ans)
}
</syntaxhighlight>
{{out}}
<pre>
Line 298:
=={{header|BASIC}}==
<
20 READ M
30 DIM D(M)
Line 304:
50 FOR J=I TO M STEP I: D(J)=D(J)+I: NEXT
60 PRINT D(I),
70 NEXT</
{{out}}
<pre> 1 3 4 7 6
Line 329:
==={{header|BASIC256}}===
<
for n = 2 to 100
p = 1 + n
Line 337:
print p; chr(9);
next n
end</
==={{header|PureBasic}}===
<
Print("1")
For n.i = 2 To 100
Line 350:
Next n
Input()
CloseConsole()</
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<
FOR n = 2 TO 100
p = 1 + n
Line 363:
PRINT p,
NEXT n
END</
==={{header|True BASIC}}===
<
FOR n = 2 To 100
LET p = 1 + n
Line 374:
PRINT p,
NEXT n
END</
==={{header|Yabasic}}===
<
for n = 2 to 100
p = 1 + n
Line 385:
print p,
next n
end</
=={{header|BCPL}}==
<
manifest $( MAXIMUM = 100 $)
Line 417:
if col rem 10 = 0 then wrch('*N')
$)
$)</
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 432:
=={{header|C}}==
{{trans|C++}}
<
// See https://en.wikipedia.org/wiki/Divisor_function
Line 468:
}
return 0;
}</
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
Line 483:
=={{header|C++}}==
<
#include <iostream>
Line 513:
std::cout << '\n';
}
}</
{{out}}
Line 531:
=={{header|CLU}}==
<
div_sums = proc (n: int) returns (array[int])
% Every number is at least divisible by 1
Line 554:
if col // 10 = 0 then stream$putc(po, '\n') end
end
end start_up</
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 568:
=={{header|Comal}}==
<
0020 //
0030 DIM divsum#(max#)
Line 579:
0100 IF i# MOD 10=0 THEN PRINT
0110 ENDFOR i#
0120 END</
{{out}}
<pre>1 3 4 7 6 12 8 15 13 18
Line 593:
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
(format t "~{~a ~}~%"
(loop for a from 1 to 100 collect
Line 599:
when (zerop (rem a b))
sum b)))
</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|Cowgol}}==
<
const MAXIMUM := 100;
Line 647:
end if;
i := i + 1;
end loop;</
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 662:
=={{header|D}}==
{{trans|C}}
<
// See https://en.wikipedia.org/wiki/Divisor_function
Line 695:
}
}
}</
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
Line 712:
{{libheader| System.SysUtils}}
{{Trans|Java}}
<syntaxhighlight lang="delphi">
program Sum_of_divisors;
Line 769:
{$IFNDEF UNIX} readln; {$ENDIF}
end.</
=={{header|F_Sharp|F#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
<
// Sum of divisors. Nigel Galloway: March 9th., 2021
let sod u=let P=primes32()
Line 780:
let n=Seq.head P in fG 1 n 1 1 n
[1..100]|>Seq.iter(sod>>printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 788:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<
sequences ;
"Sum of divisors for the first 100 positive integers:" print
100 [1,b] [ divisors sum ] map 10 group simple-table.</
{{out}}
<pre>
Line 809:
=={{header|Fermat}}==
<
for i=1 to 100 do !!Sumdiv(i) od</
=={{header|FOCAL}}==
<
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 823:
02.30 I (9-C)2.4;R
02.40 T !
02.50 S C=0</
{{out}}
<pre>= 1= 3= 4= 7= 6= 12= 8= 15= 13= 18
Line 838:
=={{header|Forth}}==
{{trans|C++}}
<
1 >r
2
Line 878:
100 print_divisor_sums
bye</
{{out}}
Line 896:
=={{header|Fortran}}==
<
implicit none
integer i, j, col, divs(100)
Line 916:
end if
30 continue
end program </
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 930:
=={{header|FreeBASIC}}==
<
print 1,
for n as uinteger = 2 to 100
Line 938:
next i
print p,
next n</
{{out}}
<pre> 1 3 4 7 6 12
Line 959:
=={{header|Frink}}==
<
print[sum[allFactors[n]] + " "]</
{{out}}
<pre>
Line 975:
=={{header|Go}}==
<
import "fmt"
Line 1,007:
}
}
}</
{{out}}
Line 1,025:
=={{header|GW-BASIC}}==
<
5 PRINT 1,
10 FOR N = 2 TO 100
Line 1,033:
50 NEXT I
60 PRINT P,
70 NEXT N</
{{out}}<pre>
1 3 4 7 6 12 8 15 13 18 12
Line 1,047:
=={{header|Haskell}}==
<
------------------------- DIVISORS -----------------------
Line 1,082:
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</
{{Out}}
<pre>Sums of divisors of [1..100]:
Line 1,130:
=={{header|Java}}==
{{trans|C++}}
<
private static long divisorSum(long n) {
var total = 1L;
Line 1,163:
}
}
}</
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
Line 1,183:
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">
# divisors as an unsorted stream
def divisors:
Line 1,211:
# The task:
[range(1; 101) | sum_of_divisors] | nwise(10) | map(lpad(4)) | join("")</
{{out}}
<pre>
Line 1,227:
=={{header|Julia}}==
<
function sumdivisors(n)
Line 1,240:
print(rpad(sumdivisors(i), 5), i % 25 == 0 ? " \n" : "")
end
</
<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,250:
=={{header|Kotlin}}==
{{trans|C++}}
<
var nn = n
var total = 1L
Line 1,292:
}
}
}</
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
Line 1,307:
=={{header|MAD}}==
<
DIMENSION DIVSUM(100)
Line 1,324:
VECTOR VALUES F = $10(I4)*$
END OF PROGRAM </
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 1,338:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<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|Nim}}==
<
proc divisors(n: Positive): seq[int] =
Line 1,353:
for n in 1..100:
stdout.write ($sum(n.divisors)).align(3), if (n + 1) mod 10 == 0: '\n' else: ' '</
{{out}}
Line 1,371:
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
<
program Sum_of_divisors;
{$IFDEF WINDOWS}}
Line 1,421:
readln;
{$ENDIF}
end.</
{{out}}
<pre>
Line 1,433:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 1,442:
say "Divisor sums - first 100:\n" .
((sprintf "@{['%4d' x 100]}", @x[0..100-1]) =~ s/(.{80})/$1\n/gr);</
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18 12 28 14 24 24 31 18 39 20 42
Line 1,451:
=={{header|PARI/GP}}==
<
{{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,457:
=={{header|Phix}}==
=== imperative ===
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,478:
=== functional ===
same output
<!--<
<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>
<!--</
=== inline assembly ===
just to show it can be done, not that many would want to, same output
<!--<
<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,518:
<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>
<!--</
=={{header|PicoLisp}}==
<
(let S 0
(for X N
Line 1,529:
(do 10
(prin (align 4 (propdiv (inc (0))))) )
(prinl) )</
{{out}}
<pre>
Line 1,545:
=={{header|PL/M}}==
<
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 1,597:
END;
CALL EXIT;
EOF</
{{out}}
<pre> 1 3 4 7 6 12 8 15 13 18
Line 1,612:
=={{header|Python}}==
===Using prime factorization===
<
assert(isinstance(n, int))
if n < 0:
Line 1,644:
if __name__ == "__main__":
print([sum_of_divisors(n) for n in range(1,101)])</
===Finding divisors efficiently===
<
assert(isinstance(n, int) and 0 < n)
ans, i, j = 0, 1, 1
Line 1,660:
if __name__ == "__main__":
print([sum_of_divisors(n) for n in range(1,101)])</
{{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,674:
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.
<
from math import floor, sqrt
Line 1,708:
# MAIN ---
if __name__ == '__main__':
main()</
=={{header|Quackery}}==
Line 1,714:
<code>factors</code> is defined at [[Factors of an integer#Quackery]].
<
[] []
Line 1,720:
[ i^ 1+ sum-of-divisors join ]
witheach [ number$ nested join ]
72 wrap$ </
{{out}}
Line 1,733:
=={{header|R}}==
This only takes one line.
<
=={{header|Racket}}==
<
(require math/number-theory)
Line 1,743:
(define (sum-of-divisors n) (apply + (divisors n)))
(displayln (for/list ((n (in-range 1 101))) (sum-of-divisors n)))</
{{out}}
Line 1,752:
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"
use Lingua::EN::Numbers;
Line 1,769:
say "\nDivisor products - first 100:\n", # ID
(1..*).map({ [×] .&divisors })[^100]\ # the task
.batch(5)».&comma».fmt("%16s").join("\n"); # display formatting</
{{out}}
<pre>Tau function - first 100:
Line 1,820:
=={{header|REXX}}==
<
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/
Line 1,848:
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. */</
{{out|output|text= when using the default input:}}
<pre>
Line 1,867:
=={{header|Ring}}==
<
see "the sums of divisors for 100 integers:" + nl
num = 0
Line 1,884:
see "" + sum + " "
next
</syntaxhighlight>
Output:
<pre>
Line 1,903:
=={{header|Ruby}}==
{{trans|C++}}
<
total = 1
power = 2
Line 1,943:
print "\n"
end
end</
{{out}}
<pre>Sum of divisors for the first 100 positive integers:
Line 1,958:
=={{header|Sidef}}==
<
{{out}}
<pre>
Line 1,965:
=={{header|Tiny BASIC}}==
<
PRINT 1
LET N = 1
Line 1,977:
30 PRINT S
IF N < 100 THEN GOTO 10
END</
=={{header|Verilog}}==
<
integer n, p, i;
Line 1,992:
$finish ;
end
endmodule</
=={{header|VTL-2}}==
<
20 M=100
30 I=1
Line 2,012:
160 ?=""
170 I=I+1
180 #=M>I*80</
{{out}}
<pre>1 3 4 7 6 12 8 15 13 18
Line 2,028:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 2,035:
Fmt.write("$3d ", Nums.sum(Int.divisors(i)))
if (i % 10 == 0) System.print()
}</
{{out}}
Line 2,053:
=={{header|XPL0}}==
<
int N, Sum, Div;
[Sum:= 0;
Line 2,068:
C:= C+1;
if rem(C/10) then ChOut(0, 9\tab\) else CrLf(0)];
]</
{{out}}
|