Largest proper divisor of n: Difference between revisions

Content added Content deleted
(→‎{{header|AppleScript}}: Corrected to make the result for 1 a task requirement rather than 1's largest proper divisor.)
Line 75: Line 75:


<lang applescript>on largestProperDivisor(n)
<lang applescript>on largestProperDivisor(n)
if (n mod 2 = 0) then
if (n mod 2 = 0) then return n div 2
return n div 2
if (n mod 3 = 0) then return n div 3
else if (n mod 3 = 0) then
if (n < 5) then return missing value
return n div 3
repeat with i from 5 to (n ^ 0.5 div 1) by 6
if (n mod i = 0) then return n div i
else
repeat with i from 5 to (n ^ 0.5 div 1) by 6
if (n mod (i + 2) = 0) then return n div (i + 2)
end repeat
if (n mod i = 0) then return n div i
return 1
if (n mod (i + 2) = 0) then return n div (i + 2)
end repeat
return 1
end if
end largestProperDivisor
end largestProperDivisor


on task(min, max)
on task(max)
script o
script o
property LPDs : {}
property LPDs : {}
Line 95: Line 92:
set w to (count (max as text)) * 2 + 3
set w to (count (max as text)) * 2 + 3
set padding to text 1 thru (w - (count (min as text)) * 2) of " "
set padding to text 1 thru (w - 2) of " "
set end of o's LPDs to "1:1" & padding
set astid to AppleScript's text item delimiters
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set AppleScript's text item delimiters to ""
set c to 0
set c to 1
repeat with n from min to max
repeat with n from 2 to max
set end of o's LPDs to text 1 thru w of ((n as text) & ":" & largestProperDivisor(n) & padding)
set end of o's LPDs to text 1 thru w of ((n as text) & ":" & largestProperDivisor(n) & padding)
set c to c + 1
set c to c + 1
Line 115: Line 113:
end task
end task


task(1, 100)</lang>
task(100)</lang>


{{output}}
{{output}}