Proper divisors: Difference between revisions

Content added Content deleted
(add REXX)
(add PL/I)
Line 160: Line 160:


Note that the first code will choose the first max, while the second chooses the last.
Note that the first code will choose the first max, while the second chooses the last.

=={{header|PL/I}}==
<lang pli>*process source xref;
(subrg):
cpd: Proc Options(main);
p9a=time();
Dcl (p9a,p9b) Pic'(9)9';
Dcl cnt(3) Bin Fixed(31) Init((3)0);
Dcl x Bin Fixed(31);
Dcl pd(300) Bin Fixed(31);
Dcl sumpd Bin Fixed(31);
Dcl npd Bin Fixed(31);
Dcl hi Bin Fixed(31) Init(0);
Dcl (xl(10),xi) Bin Fixed(31);
Dcl i Bin Fixed(31);
Do x=1 To 10;
Call proper_divisors(x,pd,npd);
Put Edit(x,' -> ',(pd(i) Do i=1 To npd))(Skip,f(2),a,10(f(2)));
End;
xi=0;
Do x=1 To 20000;
Call proper_divisors(x,pd,npd);
Select;
When(npd>hi) Do;
xi=1;
xl(1)=x;
hi=npd;
End;
When(npd=hi) Do;
xi+=1;
xl(xi)=x;
End;
Otherwise;
End;
End;
Put Edit(hi,' -> ',(xl(i) Do i=1 To xi))(Skip,f(3),a,10(f(6)));

x= 166320; Call proper_divisors(x,pd,npd);
Put Edit(x,' -> ',npd)(Skip,f(8),a,f(4));
x=1441440; Call proper_divisors(x,pd,npd);
Put Edit(x,' -> ',npd)(Skip,f(8),a,f(4));


p9b=time();
Put Edit((p9b-p9a)/1000,' seconds elapsed')(Skip,f(6,3),a);
Return;

proper_divisors: Proc(n,pd,npd);
Dcl (n,pd(300),npd) Bin Fixed(31);
Dcl (d,delta) Bin Fixed(31);
npd=0;
If n>1 Then Do;
If mod(n,2)=1 Then /* odd number */
delta=2;
Else /* even number */
delta=1;
Do d=1 To n/2 By delta;
If mod(n,d)=0 Then Do;
npd+=1;
pd(npd)=d;
End;
End;
End;
End;

End;</lang>
{{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
79 -> 15120 18480
166320 -> 159
1441440 -> 287
0.530 seconds elapsed</pre>


=={{header|Python}}==
=={{header|Python}}==