Sum of divisors: Difference between revisions

Added Easylang
(Add RPL)
(Added Easylang)
 
(8 intermediate revisions by 4 users not shown)
Line 78:
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|C++}}...via Algol W
<syntaxhighlight lang="algol68">
BEGIN # sum the divisors of the first 100 positive integers #
 
# computes the sum of the divisors of v using the prime factorisation #
PROC divisor sum = ( INT v )INT:
BEGIN
INT total := 1, power := 2, n := v;
WHILE NOT ODD n DO # Deal with powers of 2 first #
total +:= power;
power *:= 2;
n OVERAB 2
OD;
INT p := 3; # Odd prime factors up to the square root #
WHILE ( p * p ) <= n DO
INT sum := 1;
power := p;
WHILE n MOD p = 0 DO
sum +:= power;
power *:= p;
n OVERAB p
OD;
p +:= 2;
total *:= sum
OD;
IF n > 1 THEN total *:= n + 1 FI; # If n > 1 then it's prime #
total
END # divisor sum # ;
BEGIN # show the first 100 divisor sums #
INT limit = 100;
print( ( "Sum of divisors for the first ", whole( limit, 0 ), " positive integers:" ) );
FOR n TO limit DO
IF n MOD 10 = 1 THEN print( ( newline ) ) FI;
print( ( " ", whole( divisor sum( n ), -4 ) ) )
OD
END
END
</syntaxhighlight>
{{out}}
<pre>
Sum of divisors for the first 100 positive integers:
1 3 4 7 6 12 8 15 13 18
12 28 14 24 24 31 18 39 20 42
32 36 24 60 31 42 40 56 30 72
32 63 48 54 48 91 38 60 56 90
42 96 44 84 78 72 48 124 57 93
72 98 54 120 72 120 80 90 60 168
62 96 104 127 84 144 68 126 96 144
72 195 74 114 124 140 96 168 80 186
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
</pre>
 
Line 844 ⟶ 898:
{$IFNDEF UNIX} readln; {$ENDIF}
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
write 1 & " "
for n = 2 to 100
p = 1 + n
for i = 2 to n div 2
if n mod i = 0
p += i
.
.
write p & " "
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 1,042 ⟶ 1,111:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sum_of_divisors}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
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 01.png]]
In '''[https://formulae.org/?example=Sum_of_divisors this]''' page you can see the program(s) related to this task and their results.
 
'''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]]
 
[[File:Fōrmulæ - Sum of divisors 05.png]]
 
=={{header|Go}}==
Line 1,397 ⟶ 1,478:
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217</pre>
 
=={{header|Lua}}==
{{Trans|C++}}...via Algol 68
<syntaxhighlight lang="lua">
do -- sum the divisors of the first 100 positive integers
 
-- computes the sum of the divisors of v using the prime factorisation
function divisor_sum( v )
local total, power, n = 1, 2, v
while n % 2 == 0 do -- Deal with powers of 2 first
total = total + power
power = power * 2
n = math.floor( n / 2 )
end
local p = 3 -- Odd prime factors up to the square root
while ( p * p ) <= n do
local sum = 1
power = p
while n % p == 0 do
sum = sum + power
power = power * p
n = math.floor( n / p )
end
p = p + 2
total = total * sum
end
if n > 1 then total = total * ( n + 1 ) end -- If n > 1 then it's prime
return total
end
 
-- show the first 100 divisor sums
local limit = 100
io.write( "Sum of divisors for the first ", limit, " positive integers:\n" )
for n = 1, limit do
io.write( string.format( " %4d", divisor_sum( n ) ) )
if n % 10 == 0 then io.write( "\n" ) end
end
 
end
</syntaxhighlight>
{{out}}
<pre>
Sum of divisors for the first 100 positive integers:
1 3 4 7 6 12 8 15 13 18
12 28 14 24 24 31 18 39 20 42
32 36 24 60 31 42 40 56 30 72
32 63 48 54 48 91 38 60 56 90
42 96 44 84 78 72 48 124 57 93
72 98 54 120 72 120 80 90 60 168
62 96 104 127 84 144 68 126 96 144
72 195 74 114 124 140 96 168 80 186
121 126 84 224 108 132 120 180 90 234
112 168 128 144 120 252 98 171 156 217
</pre>
 
=={{header|MAD}}==
Line 1,433 ⟶ 1,568:
{{out}}
<pre>{1, 3, 4, 7, 6, 12, 8, 15, 13, 18, 12, 28, 14, 24, 24, 31, 18, 39, 20, 42, 32, 36, 24, 60, 31, 42, 40, 56, 30, 72, 32, 63, 48, 54, 48, 91, 38, 60, 56, 90, 42, 96, 44, 84, 78, 72, 48, 124, 57, 93, 72, 98, 54, 120, 72, 120, 80, 90, 60, 168, 62, 96, 104, 127, 84, 144, 68, 126, 96, 144, 72, 195, 74, 114, 124, 140, 96, 168, 80, 186, 121, 126, 84, 224, 108, 132, 120, 180, 90, 234, 112, 168, 128, 144, 120, 252, 98, 171, 156, 217}</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
divisorSum = function(n)
ans = 0
i = 1
while i * i <= n
if n % i == 0 then
ans += i
j = floor(n / i)
if j != i then ans += j
end if
i += 1
end while
return ans
end function
 
sums = []
for n in range(1, 100)
sums.push(divisorSum(n))
end for
 
print sums.join(", ")
</syntaxhighlight>
{{out}}
<pre>
1, 3, 4, 7, 6, 12, 8, 15, 13, 18, 12, 28, 14, 24, 24, 31, 18, 39, 20, 42, 32, 36, 24, 60, 31, 42, 40, 56, 30, 72, 32, 63, 48, 54, 48, 91, 38, 60, 56, 90, 42, 96, 44, 84, 78, 72, 48, 124, 57, 93, 72, 98, 54, 120, 72, 120, 80, 90, 60, 168, 62, 96, 104, 127, 84, 144, 68, 126, 96, 144, 72, 195, 74, 114, 124, 140, 96, 168, 80, 186, 121, 126, 84, 224, 108, 132, 120, 180, 90, 234, 112, 168, 128, 144, 120, 252, 98, 171, 156, 217
</pre>
 
=={{header|Nim}}==
Line 2,159 ⟶ 2,322:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int, Nums
import "./fmt" for Fmt
 
System.print("The sums of positive divisors for the first 100 positive integers are:")
2,041

edits