RosettaCodeData/Task/Proper-divisors/S-BASIC/proper-divisors.basic

64 lines
1.3 KiB
Plaintext

$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
if n < 2 then count = 0 else count = 1
if display and (count = 1) 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