Proper divisors: Difference between revisions
Content added Content deleted
(Added Seed7 example) |
(tbas) |
||
Line 4,359: | Line 4,359: | ||
10: [1, 2, 5] |
10: [1, 2, 5] |
||
15120: 79 |
15120: 79 |
||
</pre> |
|||
=={{header|tbas}}== |
|||
<lang vb> |
|||
dim _proper_divisors(100) |
|||
sub proper_divisors(n) |
|||
dim i |
|||
dim _proper_divisors_count = 0 |
|||
if n <> 1 then |
|||
for i = 1 to (n \ 2) |
|||
if n %% i = 0 then |
|||
_proper_divisors_count = _proper_divisors_count + 1 |
|||
_proper_divisors(_proper_divisors_count) = i |
|||
end if |
|||
next |
|||
end if |
|||
return _proper_divisors_count |
|||
end sub |
|||
sub show_proper_divisors(n, tabbed) |
|||
dim cnt = proper_divisors(n) |
|||
print str$(n) + ":"; tab(4);"(" + str$(cnt) + " items) "; |
|||
dim j |
|||
for j = 1 to cnt |
|||
if tabbed then |
|||
print str$(_proper_divisors(j)), |
|||
else |
|||
print str$(_proper_divisors(j)); |
|||
end if |
|||
if (j < cnt) then print ","; |
|||
next |
|||
print |
|||
end sub |
|||
dim i |
|||
for i = 1 to 10 |
|||
show_proper_divisors(i, false) |
|||
next |
|||
dim c |
|||
dim maxindex = 0 |
|||
dim maxlength = 0 |
|||
for t = 1 to 20000 |
|||
c = proper_divisors(t) |
|||
if c > maxlength then |
|||
maxindex = t |
|||
maxlength = c |
|||
end if |
|||
next |
|||
print "A maximum at "; |
|||
show_proper_divisors(maxindex, false) |
|||
</lang> |
|||
<pre> |
|||
>tbas proper_divisors.bas |
|||
1: (0 items) |
|||
2: (1 items) 1 |
|||
3: (1 items) 1 |
|||
4: (2 items) 1,2 |
|||
5: (1 items) 1 |
|||
6: (3 items) 1,2,3 |
|||
7: (1 items) 1 |
|||
8: (3 items) 1,2,4 |
|||
9: (2 items) 1,3 |
|||
10: (3 items) 1,2,5 |
|||
A maximum at 15120:(79 items) 1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,27,28,30, |
|||
35,36,40,42,45,48,54,56,60,63,70,72,80,84,90,105,108,112,120,126,135, |
|||
140,144,168,180,189,210,216,240,252,270,280,315,336,360,378,420,432, |
|||
504,540,560,630,720,756,840,945,1008,1080,1260,1512,1680,1890,2160, |
|||
2520,3024,3780,5040,7560 |
|||
</pre> |
</pre> |
||