Zumkeller numbers: Difference between revisions
m
→{{header|AppleScript}}: Incorporated "abundant number" optimisation for odd numbers.
m (→{{header|AppleScript}}: Minor tweaks, comment and label changes, optional cheat to reduce the stretch goal time.) |
m (→{{header|AppleScript}}: Incorporated "abundant number" optimisation for odd numbers.) |
||
Line 550:
=={{header|AppleScript}}==
On my machine, this takes about 0.
<lang applescript>--
on aliquotSum(n)
set limit to sqrt div 1
if (limit = sqrt) then
set sum to sum + limit
set limit to limit - 1
if (n mod i is 0) then set sum to sum + i + n div i
end repeat▼
return sum
end aliquotSum
-- Return n's proper divisors.
on properDivisors(n)
set output to {}
Line 599 ⟶ 616:
-- Is n a Zumkeller number?
on isZumkeller(n)
-- Yes if its aliquot sum is greater than or equal to it, the difference between them is even, and
-- either n is
-- The 'or equal' logic works because (below 2,000,000 at least) no odd numbers, and only the even
-- Zumkellers 6, 28, 496, and 8128, /are/ equal to their aliquot sums. The rest are all greater or smaller.
▲ end script
-- Using the aliquotSum() handler not only saves creating and looping through a list of the proper divisors
-- when n is odd, but the result also eliminates enough evens that calling properDivisors() on top of that
-- to handle what's left is still faster than using properDivisors() with all the even numbers.
▲ set sum to n
tell aliquotSum(n) to ¬
▲ repeat with thisDivisor in o's divisors
((n mod 2 = 1) or (my subsetSumsTo(my properDivisors(n), (it + n) div 2))))
▲ end repeat
▲ set halfSum to sum div 2
▲ if ((halfSum < n) or (halfSum < sum / 2)) then return false
end isZumkeller
Line 681 ⟶ 693:
end script
if (cheating) then
-- Knowing that the HCF of at least the first 200 odd Zumkellers
--
-- (Somewhere between 200 and 500, the HCF reduces to 21, so adjust accordingly.)
set zumkellers to zumkellerNumbers(40, 63, 126, no5Multiples)
else
|