Proper divisors: Difference between revisions

Added various BASIC dialects (Chipmunk Basic and Gambas)
(Added various BASIC dialects (Chipmunk Basic and Gambas))
 
(15 intermediate revisions by 8 users not shown)
Line 1,252:
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|QB64}}
<syntaxhighlight lang="qbasic">FUNCTION CountProperDivisors (number)
IF number < 2 THEN CountProperDivisors = 0
Line 1,343 ⟶ 1,344:
Igual que la entrada de FreeBASIC o PureBasic.
</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|BASIC}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 cls
110 most = 1
120 maxcount = 0
130 print "The proper divisors of the following numbers are: ";chr$(10)
140 listproperdivisors(10)
150 for n = 2 to 20000
160 rem It is extremely slow in this loop
170 count = countproperdivisors(n)
180 if count > maxcount then
190 maxcount = count
200 most = n
210 endif
220 next n
230 print
240 print most;" has the most proper divisors, namely ";maxcount
250 end
260 function countproperdivisors(number)
270 if number < 2 then countproperdivisors = 0
280 count = 0
290 for i = 1 to int(number/2)
300 if number mod i = 0 then count = count+1
310 next i
320 countproperdivisors = count
330 end function
340 sub listproperdivisors(limit)
350 if limit < 1 then exit sub
360 for i = 1 to limit
370 print using "## ->";i;
380 if i = 1 then print " (None)";
390 for j = 1 to int(i/2)
400 if i mod j = 0 then print " ";j;
410 next j
420 print
430 next i
440 end sub</syntaxhighlight>
 
==={{header|Craft Basic}}===
Line 1,415 ⟶ 1,455:
9 : 1 3
10 : 1 2 5
15120 has the most proper divisors, namely 79
</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Sub ListProperDivisors(limit As Integer)
 
If limit < 1 Then Return
For i As Integer = 1 To limit
Print Format$(i, "## ->");
If i = 1 Then
Print " (None)"
Continue
End If
For j As Integer = 1 To i \ 2
If i Mod j = 0 Then Print " "; j;
Next
Print
Next
 
End Sub
 
Function CountProperDivisors(number As Integer) As Integer
 
If number < 2 Then Return 0
Dim count As Integer = 0
For i As Integer = 1 To number \ 2
If number Mod i = 0 Then count += 1
Next
Return count
 
End Function
 
Public Sub Main()
Dim n As Integer, count As Integer, most As Integer = 1, maxCount As Integer = 0
Print "The proper divisors of the following numbers are: \n"
ListProperDivisors(10)
For n As Integer = 2 To 20000
count = CountProperDivisors(n)
If count > maxCount Then
maxCount = count
most = n
End If
Next
Print
Print most; " has the most proper divisors, namely "; maxCount
End</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|True BASIC}}===
Line 1,507 ⟶ 1,600:
Igual que la entrada de FreeBASIC o PureBasic.
</pre>
 
 
=={{header|BaCon}}==
Line 2,241 ⟶ 2,333:
10: [1, 2, 5]
15120: 79</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func[] propdivs n .
if n < 2
return [ ]
.
divs[] &= 1
sqr = sqrt n
for d = 2 to sqr
if n mod d = 0
divs[] &= d
if d <> sqr
divs[] &= n / d
.
.
.
return divs[]
.
for i to 10
print i & ":" & propdivs i
.
for i to 20000
d[] = propdivs i
if len d[] > max
max = len d[]
maxi = i
.
.
print maxi & " has " & max & " proper divisors."
</syntaxhighlight>
{{out}}
<pre>
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 ]
15120 has 79 proper divisors.
</pre>
 
=={{header|EchoLisp}}==
Line 2,761 ⟶ 2,898:
</pre>
 
=={{header|Free Pascal}}==
<syntaxhighlight lang="pascal">
Program ProperDivisors;
 
Uses fgl;
 
Type
TIntegerList = Specialize TfpgList<longint>;
 
Var list : TintegerList;
 
Function GetProperDivisors(x : longint): longint;
{this function will return the number of proper divisors
and put them in the list}
 
Var i : longint;
Begin
list.clear;
If x = 1 Then {by default 1 has no proper divisors}
GetProperDivisors := 0
Else
Begin
list.add(1); //add 1 as a proper divisor;
i := 2;
While i * i < x Do
Begin
If (x Mod i) = 0 Then //found a proper divisor
Begin
list.add(i); // add divisor
list.add(x Div i); // add result
End;
inc(i);
End;
If i*i=x Then list.add(i); //make sure to capture the sqrt only once
GetProperDivisors := list.count;
End;
End;
 
Var i,j,count,most : longint;
Begin
 
list := TIntegerList.Create;
For i := 1 To 10 Do
Begin
write(i:4,' has ', GetProperDivisors(i),' proper divisors:');
For j := 0 To pred(list.count) Do
write(list[j]:3);
writeln();
End;
count := 0; //store highest number of proper divisors
most := 0; //store number with highest number of proper divisors
For i := 1 To 20000 Do
If GetProperDivisors(i) > count Then
Begin
count := list.count;
most := i;
End;
writeln(most,' has ',count,' proper divisors');
list.free;
End.
</syntaxhighlight>
{{out}}
<pre>
1 has 0 proper divisors:
2 has 1 proper divisors: 1
3 has 1 proper divisors: 1
4 has 2 proper divisors: 1 2
5 has 1 proper divisors: 1
6 has 3 proper divisors: 1 2 3
7 has 1 proper divisors: 1
8 has 3 proper divisors: 1 2 4
9 has 2 proper divisors: 1 3
10 has 3 proper divisors: 1 2 5
15120 has 79 proper divisors
</pre>
 
=={{header|Frink}}==
Line 3,735 ⟶ 3,797:
=={{header|langur}}==
{{trans|Go}}
<syntaxhighlight lang="langur">
{{works with|langur|0.10}}
<syntaxhighlight lang="langur">val .getproper = f(.fn x): for[=[]] .i of .x \ 2 { if .x div .i: _for ~= [.i] }
val .cntproper = f(.fn x): for[=0] .i of .x \ 2 { if .x div .i: _for += 1 }
 
val .listproper = ffn(.x) {
if .x < 1: return null
for[=""] .i of .x {
writeln_for ~= $"\.{{i:2;}} -> ", .{{getproper(.i)}}\n"
}
writeln()
}
 
writeln "The proper divisors of the following numbers are :"
.writeln listproper(10)
 
var .maxmx = 0
var .most = []
for .n in 2 .. 20_000 {
val .cnt = .cntproper(.n)
if .cnt == .maxmx {
.most = more .(most, .n)
} else if .cnt > .maxmx {
.maxmx, .most = .cnt, [.n]
}
}
 
writeln $"The following number(s) <= 20000 have the most proper divisors (\.max;{{mx}})"
writeln .most</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 5,888 ⟶ 5,950:
9 -> 1 3
10 -> 1 2 5
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ DIVIS REVLIST TAIL REVLIST <span style="color:grey">@ or DIVIS 1 OVER SIZE 1 - SUB</span>
≫ '<span style="color:blue">PDIVIS</span>' STO
≪ 0 → max n
≪ 0
1 max '''FOR''' j
j <span style="color:blue">PDIVIS</span> SIZE
'''IF''' DUP2 < '''THEN''' SWAP j 'n' STO '''END'''
DROP
'''NEXT'''
DROP n DUP <span style="color:blue">PDIVIS</span> SIZE
≫ ≫ '<span style="color:blue">TASK2</span>' STO
 
≪ n <span style="color:blue">PDIVIS</span> ≫ 'n' 1 10 1 SEQ
20000 <span style="color:blue">TASK2</span>
{{out}}
<pre>
3: {{} {1} {1} {1 2} {1} {1 2 3} {1} {1 2 4} {1 3} {1 2 5}}
2: 15120
1: 39
</pre>
 
Line 6,206 ⟶ 6,292:
{{trans|Raku}}
<syntaxhighlight lang="ruby">func propdiv (n) {
n.divisors.slicefirst(0, -21)
}
 
Line 6,548 ⟶ 6,634:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Int
 
for (i in 1..10) System.print("%(Fmt.d(2, i)) -> %(Int.properDivisors(i))")
2,169

edits