Proper divisors: Difference between revisions

Content added Content deleted
No edit summary
Line 3,973: Line 3,973:
Proper divisors of 10: [1, 2, 5]
Proper divisors of 10: [1, 2, 5]
In 1 to 20000, 15120 has the most proper divisors at 79
In 1 to 20000, 15120 has the most proper divisors at 79
</pre>

=={{header|S-Basic}}==
<lang Basic>
$lines

$constant false = 0
$constant true = FFFFH

rem - compute p mod q
function mod(p, q = integer) = integer
end = p - q * (p/q)

rem - count, and optionally display, proper divisors of n
function divisors(n, display = integer) = integer
var i, limit, count, start, delta = integer
if mod(n, 2) = 0 then
begin
start = 2
delta = 1
end
else
begin
start = 3
delta = 2
end
count = 1
if display then print using "#####"; 1;
i = start
limit = n / start
while i <= limit do
begin
if mod(n, i) = 0 then
begin
if display then print using "#####"; i;
count = count + 1
end
i = i + delta
if count = 1 then limit = n / i
end
if display then print
end = count

rem - main program begins here
var i, ndiv, highdiv, highnum = integer

print "Proper divisors of first 10 numbers:"
for i = 1 to 10
print using "### : "; i;
ndiv = divisors(i, true)
next i

print "Searching for number with most divisors ..."
highdiv = 1
highnum = 1
for i = 1 to 20000
ndiv = divisors(i, false)
if ndiv > highdiv then
begin
highdiv = ndiv
highnum = i
end
next i
print "Searched up to"; i
print highnum; " has the most divisors: "; highdiv

end
</lang>
{{out}}
<pre>
Proper divisors of first 10 numbers:
1 : 1
2 : 1
3 : 1
4 : 1 2
5 : 1
6 : 1 2 3
7 : 1
8 : 1 2 4
9 : 1 3
10 : 1 2 5
Searching for number with most divisors ...
Searched up to 20000
15120 has the most divisors: 79
</pre>
</pre>