Zumkeller numbers: Difference between revisions

Content deleted Content added
Nig (talk | contribs)
Added AppleScript.
Nig (talk | contribs)
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 115 for the stretch goal!
This takes about 0.38 seconds to execute the two main searches — then a further 109 for the stretch goal!


<lang applescript>on factors(n)
<lang applescript>on properDivisors(n)
set output to {}
set output to {}
set sqrt to n ^ 0.5
if (n > 1) then
set limit to sqrt div 1
set sqrt to n ^ 0.5
if (limit = sqrt) then
set limit to sqrt div 1
set end of output to limit
if (limit = sqrt) then
set limit to limit - 1
set end of output to limit
set limit to limit - 1
end if
repeat with i from limit 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
end if
repeat with i from limit to 1 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
return output
return output
end factors
end properDivisors


on containsSubtotal(lst, subtotal, i, j)
on isZumkeller(n)
script o
script o
property divisors : factors(n)
property l : lst
on semiperfect(n, i)
on cs(subtotal, j)
set d to item i of my divisors
set n to item j of my l
if (n = d) then return true
if (n = subtotal) then return true
if (i = 1) then return false
if (j = i) then return false
set i to i - 1
set j to j - 1
return (((n > d) and (semiperfect(n - d, i))) or (semiperfect(n, i)))
return (((subtotal > n) and (cs(subtotal - n, j))) or (cs(subtotal, j)))
end semiperfect
end cs
end script
end script
set sum to 0
return o's cs(subtotal, j)
end containsSubtotal

on isZumkeller(n)
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 semiperfect(halfSum, (count o's divisors) - 1)))
return ((halfSum ≥ n) and (halfSum as integer = halfSum) and (containsSubtotal(o's divisors, halfSum, 1, (count o's divisors))))
end isZumkeller
end isZumkeller


-- Task code:
on zumkellerNumbers(target, n, interval, customiser)
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 constraints on numbers tested.
-- 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 ((customiser's OK(n)) and (isZumkeller(n))) then
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


-- Task code:
on format(resultList, heading, resultsPerLine, separator)
on format(resultList, heading, resultsPerLine, separator)
script o
script o
Line 662: Line 673:


{{output}}
{{output}}
<lang applescript>Result:
<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