Jump to content

Proper divisors: Difference between revisions

no edit summary
No edit summary
Line 616:
<pre>{15120, 79}</pre>
 
=={{header|Oberon-2}}==
<lang oberon2>
MODULE ProperDivisors;
IMPORT
Out;
 
CONST
initialSize = 128;
TYPE
Result* = POINTER TO ResultDesc;
ResultDesc = RECORD
found-: LONGINT; (* number of slots in pd *)
pd-: POINTER TO ARRAY OF LONGINT;
cap: LONGINT; (* Capacity *)
END;
VAR
i,found,max,idxMx: LONGINT;
mx: ARRAY 32 OF LONGINT;
rs: Result;
 
PROCEDURE (r: Result) Init(size: LONGINT);
BEGIN
r.found := 0;
r.cap := size;
NEW(r.pd,r.cap);
END Init;
 
PROCEDURE (r: Result) Add(n: LONGINT);
BEGIN
(* Out.String("--->");Out.LongInt(n,0);Out.String(" At: ");Out.LongInt(r.found,0);Out.Ln; *)
IF (r.found < LEN(r.pd^) - 1) THEN
r.pd[r.found] := n;
ELSE
(* expand pd for more room *)
END;
INC(r.found);
END Add;
 
PROCEDURE (r:Result) Show();
VAR
i: LONGINT;
BEGIN
Out.String("(Result:");Out.LongInt(r.found + 1,0);(* Out.String("/");Out.LongInt(r.cap,0);*)
Out.String("-");
IF r.found > 0 THEN
FOR i:= 0 TO r.found - 1 DO
Out.LongInt(r.pd[i],0);
IF i = r.found - 1 THEN Out.Char(')') ELSE Out.Char(',') END
END
END;
Out.Ln
END Show;
 
PROCEDURE (r:Result) Reset();
BEGIN
r.found := 0;
END Reset;
 
PROCEDURE GetFor(n: LONGINT;VAR rs: Result);
VAR
i: LONGINT;
BEGIN
IF n > 1 THEN
rs.Add(1);i := 2;
WHILE (i < n) DO
IF (n MOD i) = 0 THEN rs.Add(i) END;
INC(i)
END
END;
END GetFor;
BEGIN
NEW(rs);rs.Init(initialSize);
FOR i := 1 TO 10 DO
Out.LongInt(i,4);Out.Char(':');
GetFor(i,rs);
rs.Show();
rs.Reset();
END;
Out.LongInt(100,4);Out.Char(':');GetFor(100,rs);rs.Show();rs.Reset();
max := 0;idxMx := 0;found := 0;
FOR i := 1 TO 20000 DO
GetFor(i,rs);
IF rs.found > max THEN
idxMx:= 0;mx[idxMx] := i;max := rs.found
ELSIF rs.found = max THEN
INC(idxMx);mx[idxMx] := i
END;
rs.Reset()
END;
Out.String("Found: ");Out.LongInt(idxMx + 1,0);
Out.String(" Numbers with most proper divisors ");
Out.LongInt(max,0);Out.String(": ");Out.Ln;
FOR i := 0 TO idxMx DO
Out.LongInt(mx[i],0);Out.Ln
END
END ProperDivisors.
</lang>
{{out}}
<pre>
1:(Result:1-
2:(Result:2-1)
3:(Result:2-1)
4:(Result:3-1,2)
5:(Result:2-1)
6:(Result:4-1,2,3)
7:(Result:2-1)
8:(Result:4-1,2,4)
9:(Result:3-1,3)
10:(Result:4-1,2,5)
100:(Result:9-1,2,4,5,10,20,25,50)
Found: 2 Numbers with most proper divisors 79:
15120
18480
</pre>
=={{header|Oforth}}==
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.