Proper divisors: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) |
(→{{header|AppleScript}}: Added idiomatic solution.) |
||
Line 486: | Line 486: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
===Functional=== |
|||
{{Trans|JavaScript}} |
{{Trans|JavaScript}} |
||
<lang AppleScript>-- PROPER DIVISORS ----------------------------------------------------------- |
<lang AppleScript>-- PROPER DIVISORS ----------------------------------------------------------- |
||
Line 623: | Line 624: | ||
{num:10, divisors:{1, 2, 5}}}, |
{num:10, divisors:{1, 2, 5}}}, |
||
mostDivisors:{num:18480, divisors:79}}</lang> |
mostDivisors:{num:18480, divisors:79}}</lang> |
||
---- |
|||
===Idiomatic=== |
|||
<lang applescript>on properDivisors(n) |
|||
set output to {} |
|||
if (n > 1) then |
|||
set sqrt to n ^ 0.5 |
|||
if (sqrt mod 1 is 0) then |
|||
set end of output to sqrt as integer |
|||
set sqrt to sqrt - 1 |
|||
end if |
|||
repeat with i from (sqrt div 1) to 2 by -1 |
|||
if (n mod i is 0) then |
|||
set beginning of output to i |
|||
set end of output to n div i |
|||
end if |
|||
end repeat |
|||
set beginning of output to 1 |
|||
end if |
|||
return output |
|||
end properDivisors |
|||
-- Task code. |
|||
local output, astid, i, maxPDs, maxPDNums, pdCount |
|||
set output to {} |
|||
set astid to AppleScript's text item delimiters |
|||
set AppleScript's text item delimiters to ", " |
|||
repeat with i from 1 to 10 |
|||
set end of output to (i as text) & "'s proper divisors: {" & properDivisors(i) & "}" |
|||
end repeat |
|||
set maxPDs to 0 |
|||
set maxPDNums to {} |
|||
repeat with i from 1 to 20000 |
|||
set pdCount to (count properDivisors(i)) |
|||
if (pdCount > maxPDs) then |
|||
set maxPDs to pdCount |
|||
set maxPDNums to {i} |
|||
else if (pdCount = maxPDs) then |
|||
set end of maxPDNums to i |
|||
end if |
|||
end repeat |
|||
set end of output to linefeed & "Largest number of proper divisors in any number from 1 to 20,000: " & maxPDs |
|||
set end of output to "Numbers with this many: " & maxPDNums |
|||
set AppleScript's text item delimiters to linefeed |
|||
set output to output as text |
|||
set AppleScript's text item delimiters to astid |
|||
return output</lang> |
|||
{{output}} |
|||
<lang applescript>"1's proper divisors: {} |
|||
2's proper divisors: {1} |
|||
3's proper divisors: {1} |
|||
4's proper divisors: {1, 2} |
|||
5's proper divisors: {1} |
|||
6's proper divisors: {1, 2, 3} |
|||
7's proper divisors: {1} |
|||
8's proper divisors: {1, 2, 4} |
|||
9's proper divisors: {1, 3} |
|||
10's proper divisors: {1, 2, 5} |
|||
Largest number of proper divisors for any number from 1 to 20,000: 79 |
|||
Numbers with this many: 15120, 18480"</lang> |
|||
=={{header|Arc}}== |
=={{header|Arc}}== |