Amicable pairs: Difference between revisions
Content added Content deleted
(Initial FutureBasic task solution added) |
(Added Chipmunk Basic, Gambas, QBasic and True BASIC) |
||
Line 1,383: | Line 1,383: | ||
12285 and 14595 |
12285 and 14595 |
||
17296 and 18416</pre> |
17296 and 18416</pre> |
||
==={{header|Chipmunk Basic}}=== |
|||
{{works with|Chipmunk Basic|3.6.4}} |
|||
<syntaxhighlight lang="qbasic">100 cls : rem 10 HOME for Applesoft BASIC |
|||
110 print "The pairs of amicable numbers below 20,000 are :" |
|||
120 print |
|||
130 size = 18500 |
|||
140 for n = 1 to size |
|||
150 m = amicable(n) |
|||
160 if m > n and amicable(m) = n then |
|||
170 print using "#####";n; |
|||
180 print " and "; |
|||
190 print using "#####";m |
|||
200 endif |
|||
210 next |
|||
220 end |
|||
230 function amicable(nr) |
|||
240 suma = 1 |
|||
250 for d = 2 to sqr(nr) |
|||
260 if nr mod d = 0 then suma = suma+d+nr/d |
|||
270 next |
|||
280 amicable = suma |
|||
290 end function</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as FreeBASIC entry.</pre> |
|||
==={{header|Gambas}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="vbnet">Public sum[19999] As Integer |
|||
Public Sub Main() |
|||
Dim n As Integer, f As Integer |
|||
For n = 0 To 19998 |
|||
sum[n] = SumProperDivisors(n) |
|||
Next |
|||
Print "The pairs of amicable numbers below 20,000 are :\n" |
|||
For n = 0 To 19998 |
|||
' f = SumProperDivisors(n) |
|||
f = sum[n] |
|||
If f <= n Or f < 1 Or f > 19999 Then Continue |
|||
If f = sum[n] And n = sum[f] Then |
|||
Print Format$(Str$(n), "#####"); " And "; Format$(Str$(sum[n]), "#####") |
|||
End If |
|||
Next |
|||
End |
|||
Function SumProperDivisors(number As Integer) As Integer |
|||
If number < 2 Then Return 0 |
|||
Dim sum As Integer = 0 |
|||
For i As Integer = 1 To number \ 2 |
|||
If number Mod i = 0 Then sum += i |
|||
Next |
|||
Return sum |
|||
End Function</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as FreeBASIC entry.</pre> |
|||
==={{header|QBasic}}=== |
|||
{{works with|QBasic|1.1}} |
|||
{{works with|QuickBasic|4.5}} |
|||
<syntaxhighlight lang="qbasic">FUNCTION amicable (nr) |
|||
suma = 1 |
|||
FOR d = 2 TO SQR(nr) |
|||
IF nr MOD d = 0 THEN suma = suma + d + nr / d |
|||
NEXT |
|||
amicable = suma |
|||
END FUNCTION |
|||
PRINT "The pairs of amicable numbers below 20,000 are :" |
|||
PRINT |
|||
size = 18500 |
|||
FOR n = 1 TO size |
|||
m = amicable(n) |
|||
IF m > n AND amicable(m) = n THEN |
|||
PRINT USING "##### and #####"; n; m |
|||
END IF |
|||
NEXT |
|||
END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as FreeBASIC entry.</pre> |
|||
==={{header|True BASIC}}=== |
|||
<syntaxhighlight lang="qbasic">FUNCTION amicable(nr) |
|||
LET suma = 1 |
|||
FOR d = 2 TO SQR(nr) |
|||
IF REMAINDER(nr, d) = 0 THEN |
|||
LET suma = suma + d + nr / d |
|||
END IF |
|||
NEXT d |
|||
LET amicable = suma |
|||
END FUNCTION |
|||
PRINT "The pairs of amicable numbers below 20,000 are :" |
|||
PRINT |
|||
LET size = 18500 |
|||
FOR n = 1 TO size |
|||
LET m = amicable(n) |
|||
IF m > n AND amicable(m) = n THEN PRINT USING "##### and #####": n, m |
|||
NEXT n |
|||
END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as FreeBASIC entry.</pre> |
|||
=={{header|BCPL}}== |
=={{header|BCPL}}== |