Zumkeller numbers: Difference between revisions
Content deleted Content added
Added AppleScript. |
m →{{header|AppleScript}}: Pulled out the subtotal check to a separate handler. Slight speed improvement by only getting proper divisors and knowing the missing one. |
||
Line 550: | Line 550: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
This takes about 0.38 seconds to execute the two main searches — then a further |
This takes about 0.38 seconds to execute the two main searches — then a further 109 for the stretch goal! |
||
<lang applescript>on |
<lang applescript>on properDivisors(n) |
||
set output to {} |
set output to {} |
||
if (n > 1) then |
|||
set |
set sqrt to n ^ 0.5 |
||
set limit to sqrt div 1 |
|||
if (limit = sqrt) then |
|||
set |
set end of output to limit |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
set end of output to n div i |
|||
end if |
|||
⚫ | |||
set beginning of output to 1 |
|||
end if |
end if |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return output |
return output |
||
end |
end properDivisors |
||
on containsSubtotal(lst, subtotal, i, j) |
|||
⚫ | |||
script o |
script o |
||
property |
property l : lst |
||
on |
on cs(subtotal, j) |
||
set |
set n to item j of my l |
||
if (n = |
if (n = subtotal) then return true |
||
if ( |
if (j = i) then return false |
||
set |
set j to j - 1 |
||
return ((( |
return (((subtotal > n) and (cs(subtotal - n, j))) or (cs(subtotal, j))) |
||
end |
end cs |
||
end script |
end script |
||
return o's cs(subtotal, j) |
|||
end containsSubtotal |
|||
⚫ | |||
script o |
|||
property divisors : properDivisors(n) |
|||
end script |
|||
set sum to n |
|||
repeat with thisDivisor in o's divisors |
repeat with thisDivisor in o's divisors |
||
set sum to sum + thisDivisor |
set sum to sum + thisDivisor |
||
Line 591: | Line 602: | ||
set halfSum to sum / 2 |
set halfSum to sum / 2 |
||
return ((halfSum ≥ n) and (halfSum as integer = halfSum) and (o's |
return ((halfSum ≥ n) and (halfSum as integer = halfSum) and (containsSubtotal(o's divisors, halfSum, 1, (count o's divisors)))) |
||
end isZumkeller |
end isZumkeller |
||
⚫ | |||
on zumkellerNumbers(target, n, interval, |
on zumkellerNumbers(target, n, interval, filter) |
||
-- Params: how many required, first number to test, interval between numbers tested, |
-- Params: how many required, first number to test, interval between numbers tested, |
||
-- script object imposing any further |
-- script object imposing any further restrictions on the numbers. |
||
script o |
script o |
||
property zumkellers : {} |
property zumkellers : {} |
||
Line 603: | Line 615: | ||
set counter to 0 |
set counter to 0 |
||
repeat until (counter = target) |
repeat until (counter = target) |
||
if (( |
if ((filter's OK(n)) and (isZumkeller(n))) then |
||
set end of o's zumkellers to n |
set end of o's zumkellers to n |
||
set counter to counter + 1 |
set counter to counter + 1 |
||
Line 613: | Line 625: | ||
end zumkellerNumbers |
end zumkellerNumbers |
||
⚫ | |||
on format(resultList, heading, resultsPerLine, separator) |
on format(resultList, heading, resultsPerLine, separator) |
||
script o |
script o |
||
Line 662: | Line 673: | ||
{{output}} |
{{output}} |
||
<lang applescript> |
<lang applescript>"1st 220 Zumkeller numbers: |
||
"1st 220 Zumkeller numbers: |
|||
6 12 20 24 28 30 40 42 48 54 56 60 66 70 78 80 84 88 90 96 |
6 12 20 24 28 30 40 42 48 54 56 60 66 70 78 80 84 88 90 96 |
||
102 104 108 112 114 120 126 132 138 140 150 156 160 168 174 176 180 186 192 198 |
102 104 108 112 114 120 126 132 138 140 150 156 160 168 174 176 180 186 192 198 |