Proper divisors: Difference between revisions
Content added Content deleted
PatGarrett (talk | contribs) m (→{{header|360 Assembly}}: Superfluous blanks suppressed) |
(Added Lua version) |
||
Line 1,329: | Line 1,329: | ||
[15120,18480] have the maximum proper divisor count of 79. |
[15120,18480] have the maximum proper divisor count of 79. |
||
</pre> |
</pre> |
||
=={{header|Lua}}== |
|||
<lang Lua>-- Return a table of the proper divisors of n |
|||
function propDivs (n) |
|||
local divs, sqr = {1}, math.sqrt(n) |
|||
for d = 2, sqr do |
|||
if n % d == 0 then |
|||
table.insert(divs, d) |
|||
if d ~= sqr then table.insert(divs, n/d) end |
|||
end |
|||
end |
|||
table.sort(divs) |
|||
return divs |
|||
end |
|||
-- Show n followed by all values in t |
|||
function show (n, t) |
|||
io.write(n .. ":\t") |
|||
for _, v in pairs(t) do io.write(v .. " ") end |
|||
print() |
|||
end |
|||
-- Main procedure |
|||
local mostDivs, numDivs, answer = 0 |
|||
for i = 1, 10 do show(i, propDivs(i)) end |
|||
for i = 1, 20000 do |
|||
numDivs = #propDivs(i) |
|||
if numDivs > mostDivs then |
|||
mostDivs = numDivs |
|||
answer = i |
|||
end |
|||
end |
|||
print(answer .. " has " .. mostDivs .. " proper divisors.")</lang> |
|||
{{out}} |
|||
<pre>1: 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 |
|||
15120 has 79 proper divisors.</pre> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |