Proper divisors: Difference between revisions
Content added Content deleted
(Added XPL0 example.) |
(Proper divisors in various BASIC dialents) |
||
Line 1,248: | Line 1,248: | ||
18480 79 divisors not shown |
18480 79 divisors not shown |
||
</pre> |
</pre> |
||
=={{header|BASIC}}== |
|||
{{works with|QBasic|1.1}} |
|||
{{works with|QuickBasic|4.5}} |
|||
<lang qbasic>FUNCTION CountProperDivisors (number) |
|||
IF number < 2 THEN CountProperDivisors = 0 |
|||
count = 0 |
|||
FOR i = 1 TO number \ 2 |
|||
IF number MOD i = 0 THEN count = count + 1 |
|||
NEXT i |
|||
CountProperDivisors = count |
|||
END FUNCTION |
|||
SUB ListProperDivisors (limit) |
|||
IF limit < 1 THEN EXIT SUB |
|||
FOR i = 1 TO limit |
|||
PRINT USING "## ->"; i; |
|||
IF i = 1 THEN PRINT " (None)"; |
|||
FOR j = 1 TO i \ 2 |
|||
IF i MOD j = 0 THEN PRINT " "; j; |
|||
NEXT j |
|||
PRINT |
|||
NEXT i |
|||
END SUB |
|||
most = 1 |
|||
maxCount = 0 |
|||
PRINT "The proper divisors of the following numbers are: "; CHR$(10) |
|||
ListProperDivisors (10) |
|||
FOR n = 2 TO 20000 |
|||
'' It is extremely slow in this loop |
|||
count = CountProperDivisors(n) |
|||
IF count > maxCount THEN |
|||
maxCount = count |
|||
most = n |
|||
END IF |
|||
NEXT n |
|||
PRINT |
|||
PRINT most; " has the most proper divisors, namely "; maxCount |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
Igual que la entrada de FreeBASIC o PureBasic. |
|||
</pre> |
|||
==={{header|BASIC256}}=== |
|||
<lang BASIC256>subroutine ListProperDivisors(limit) |
|||
if limit < 1 then return |
|||
for i = 1 to limit |
|||
print i; " ->"; |
|||
if i = 1 then |
|||
print " (None)" |
|||
continue for |
|||
end if |
|||
for j = 1 to i \ 2 |
|||
if i mod j = 0 then print " "; j; |
|||
next j |
|||
print |
|||
next i |
|||
end subroutine |
|||
function CountProperDivisors(number) |
|||
if number < 2 then return 0 |
|||
dim cont = 0 |
|||
for i = 1 to number \ 2 |
|||
if number mod i = 0 then cont += 1 |
|||
next i |
|||
return cont |
|||
end function |
|||
most = 1 |
|||
maxCount = 0 |
|||
print "The proper divisors of the following numbers are:" |
|||
print |
|||
call ListProperDivisors(10) |
|||
for n = 2 to 20000 |
|||
cont = CountProperDivisors(n) |
|||
if cont > maxCount then |
|||
maxCount = cont |
|||
most = n |
|||
end if |
|||
next n |
|||
print |
|||
print most; " has the most proper divisors, namely "; maxCount |
|||
end</lang> |
|||
{{out}} |
|||
<pre> |
|||
Igual que la entrada de FreeBASIC o PureBasic. |
|||
</pre> |
|||
==={{header|True BASIC}}=== |
|||
<lang qbasic>FUNCTION CountProperDivisors (number) |
|||
IF number < 2 THEN LET CountProperDivisors = 0 |
|||
LET count = 0 |
|||
FOR i = 1 TO INT(number / 2) |
|||
IF MOD(number, i) = 0 THEN LET count = count + 1 |
|||
NEXT i |
|||
LET CountProperDivisors = count |
|||
END FUNCTION |
|||
SUB ListProperDivisors (limit) |
|||
IF limit < 1 THEN EXIT SUB |
|||
FOR i = 1 TO limit |
|||
PRINT i; " ->"; |
|||
IF i = 1 THEN PRINT " (None)"; |
|||
FOR j = 1 TO INT(i / 2) |
|||
IF MOD(i, j) = 0 THEN PRINT " "; j; |
|||
NEXT j |
|||
PRINT |
|||
NEXT i |
|||
END SUB |
|||
LET most = 1 |
|||
LET maxCount = 0 |
|||
PRINT "The proper divisors of the following numbers are: "; CHR$(10) |
|||
CALL LISTPROPERDIVISORS (10) |
|||
FOR n = 2 TO 20000 |
|||
LET count = CountProperDivisors(n) |
|||
IF count > maxCount THEN |
|||
LET maxCount = count |
|||
LET most = n |
|||
END IF |
|||
NEXT n |
|||
PRINT |
|||
PRINT most; "has the most proper divisors, namely"; maxCount |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
Igual que la entrada de FreeBASIC o PureBasic. |
|||
</pre> |
|||
==={{header|Yabasic}}=== |
|||
<lang yabasic>sub ListProperDivisors(limit) |
|||
if limit < 1 then return : fi |
|||
for i = 1 to limit |
|||
print i, " ->"; |
|||
if i = 1 then |
|||
print " (None)" |
|||
continue //for |
|||
endif |
|||
for j = 1 to int(i / 2) |
|||
if mod(i, j) = 0 then print " ", j; : fi |
|||
next j |
|||
print |
|||
next i |
|||
end sub |
|||
sub CountProperDivisors(number) |
|||
if number < 2 then return 0 : fi |
|||
count = 0 |
|||
for i = 1 to int(number / 2) |
|||
if mod(number, i) = 0 then count = count + 1 : fi |
|||
next i |
|||
return count |
|||
end sub |
|||
most = 1 |
|||
maxCount = 0 |
|||
print "The proper divisors of the following numbers are: \n" |
|||
ListProperDivisors(10) |
|||
for n = 2 to 20000 |
|||
count = CountProperDivisors(n) |
|||
if count > maxCount then |
|||
maxCount = count |
|||
most = n |
|||
endif |
|||
next n |
|||
print |
|||
print most, " has the most proper divisors, namely ", maxCount |
|||
end</lang> |
|||
{{out}} |
|||
<pre> |
|||
Igual que la entrada de FreeBASIC o PureBasic. |
|||
</pre> |
|||
=={{header|BaCon}}== |
=={{header|BaCon}}== |