Proper divisors: Difference between revisions

Content added Content deleted
(→‎Python: The Simple Way: straightened up the sloppy code)
(Added solution for Action!)
Line 194: Line 194:
10 has 3 proper divisors: 1 2 5
10 has 3 proper divisors: 1 2 5
15120 has 79 proper divisors.
15120 has 79 proper divisors.
</pre>

=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
<lang Action!>BYTE FUNC CountDivisors(INT a)
INT i
BYTE prod,count

prod=1 count=0
WHILE a MOD 2=0
DO
count==+1
a==/2
OD
prod==*(1+count)

i=3
WHILE i*i<=a
DO
count=0
WHILE a MOD i=0
DO
count==+1
a==/i
OD
prod==*(1+count)
i==+2
OD

IF a>2 THEN
prod==*2
FI
RETURN (prod-1)

BYTE FUNC GetDivisors(INT a INT ARRAY divisors)
INT i,max
BYTE count

max=a/2
count=0
FOR i=1 TO max
DO
IF a MOD i=0 THEN
divisors(count)=i
count==+1
FI
OD
RETURN (count)

PROC Main()
INT i,j,count,max,ind,st,range
INT ARRAY divisors(100)
BYTE perc

FOR i=1 TO 10
DO
count=GetDivisors(i,divisors)
PrintF("%I has %I proper divisors: [",i,count)
FOR j=0 TO count-1
DO
PrintI(divisors(j))
IF j<count-1 THEN
Put(32)
FI
OD
PrintE("]")
OD

PutE() PrintE("Searching for max number of divisors:")
range=20000
max=0 ind=0 perc=0 st=range/100
FOR i=1 TO range
DO
IF i MOD st=0 THEN
PrintB(perc) Put('%) PutE() Put(28)
perc==+1
FI
count=CountDivisors(i)
IF count>max THEN
max=count ind=i
FI
OD
PrintF("%I has %I proper divisors%E",ind,max)
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Proper_divisors.png Screenshot from Atari 8-bit computer]
<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]

Searching for max number of divisors:
15120 has 79 proper divisors
</pre>
</pre>