Zumkeller numbers: Difference between revisions
m
→{{header|AppleScript}}: Revised subset sum handler. Some comment work..
Alextretyak (talk | contribs) (Added 11l) |
m (→{{header|AppleScript}}: Revised subset sum handler. Some comment work..) |
||
Line 702:
-- Does a subset of the given list of numbers add up to the target value?
on
script o
property lst :
property someNegatives : false
on
repeat while (i > 1)
set n to item i of my lst
set i to i - 1
if ((n = target) or (((n < target) or (someNegatives)) and (ssp(target - n, i)))) then return true
else if (n = target) then▼
end if▼
end repeat
return (target = beginning of my lst)
end
end script
-- The search can be more efficient if it's known the list contains no negatives.
repeat with n in o's lst
set o's someNegatives to true
exit repeat
end repeat
return o's
end subsetOf:sumsTo:
-- 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 odd or a subset of its proper divisors sums to half the
-- Using aliquotSum() to get the divisor sum and then calling properDivisors() too if a list's actually
-- needed is generally faster than using properDivisors() in the first place and summing the result.
▲ tell aliquotSum(n) to ¬
▲ return ((it ≥ n) and ((it - n) mod 2 = 0) and ¬
end isZumkeller
Line 802 ⟶ 803:
end script
if (cheating) then
-- Knowing that the HCF of
-- is 63,
--
-- (See Horsth's comments on the Talk page.)
set zumkellers to zumkellerNumbers(40, 63, 126, no5Multiples)
else
|