Proper divisors: Difference between revisions

(9 intermediate revisions by 6 users not shown)
Line 2,242:
10: [1, 2, 5]
15120: 79</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc propdivs n . divs[] .
divs[] = [ ]
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
.
.
.
.
for i to 10
propdivs i d[]
write i & ":"
print d[]
.
for i to 20000
propdivs i d[]
if len d[] > max
max = len d[]
maxi = i
.
.
print maxi & " has " & max & " proper divisors."
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 2,762 ⟶ 2,795:
</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,736 ⟶ 3,694:
=={{header|langur}}==
{{trans|Go}}
<syntaxhighlight lang="langur">val .getproper = fn(.x) { for[=[]] .i of .x \ 2 { if .x div .i: _for ~= [.i] } }
{{works with|langur|0.10}}
<syntaxhighlight lang="langur">val .getpropercntproper = ffn(.x) { for[=[]0] .i of .x \ 2 { if .x div .i: _for ~+= [.i]1 } }
val .cntproper = f(.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 .max = 0
Line 5,889 ⟶ 5,845:
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,207 ⟶ 6,187:
{{trans|Raku}}
<syntaxhighlight lang="ruby">func propdiv (n) {
n.divisors.slicefirst(0, -21)
}
 
Line 6,549 ⟶ 6,529:
{{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))")
885

edits