Jump to content

Zumkeller numbers: Difference between revisions

m
→‎{{header|AppleScript}}: Revised subset sum handler. Some comment work..
(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 subsetSumsTo(listOfNumbers,subsetOf:numberList sumsTo:target)
script o
property lst : listOfNumbersnumberList
property someNegatives : false
on recursionssp(target, i)
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
if (recursion(target - n, i)) then return true
else if (n = target) then
return true
end if
end repeat
return (target = beginning of my lst)
end recursionssp
end script
-- The search can be more efficient if it's known the list contains no negatives.
repeat with n in o's lst
else if (n =< target0) then
set o's someNegatives to true
exit repeat
end if
end repeat
return o's recursionssp(target, count o's lst)
end subsetOf:sumsTo:
end subsetSumsTo
 
-- 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 averagesum of itthe divisors and all its proper divisorsit.
-- Using aliquotSum() to get the divisor sum and then calling properDivisors() too if a list's actually
-- The 'or equal' logic works because (below 2,000,000 at least) no odd numbers, and only the even
-- needed is generally faster than using properDivisors() in the first place and summing the result.
-- Zumkellers 6, 28, 496, and 8128, /are/ equal to their aliquot sums. The rest are all greater or smaller.
tellset sum to aliquotSum(n) to ¬
-- Using the aliquotSum() handler not only saves creating and looping through a list of the proper divisors
return ((itsum ≥ n) and ((itsum - n) mod 2 = 0) and ¬
-- when n is odd, but the result also eliminates enough evens that calling properDivisors() on top of that
-- to handle what's left((n ismod still2 faster= than1) usingor (my subsetOf:(properDivisors(n)) withsumsTo:((sum all+ then) evendiv numbers.2))))
tell aliquotSum(n) to ¬
return ((it ≥ n) and ((it - n) mod 2 = 0) and ¬
((n mod 2 = 1) or (my subsetSumsTo(my properDivisors(n), (it + n) div 2))))
end isZumkeller
 
Line 802 ⟶ 803:
end script
if (cheating) then
-- Knowing that the HCF of at least the first 200203 odd Zumkellers which don'tnot endending with 5
-- is 63, onlyjust check 63 and each 126th number thereafter.
-- (SomewhereFor betweenthe 204th - 200907th andsuch 500numbers, the HCF reduces to 21, so adjust accordingly.)
-- (See Horsth's comments on the Talk page.)
set zumkellers to zumkellerNumbers(40, 63, 126, no5Multiples)
else
557

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.