How to resolve the algorithm Zumkeller numbers step by step in the zkl programming language
How to resolve the algorithm Zumkeller numbers step by step in the zkl programming language
Table of Contents
Problem Statement
Zumkeller numbers are the set of numbers whose divisors can be partitioned into two disjoint sets that sum to the same value. Each sum must contain divisor values that are not in the other sum, and all of the divisors must be in one or the other. There are no restrictions on how the divisors are partitioned, only that the two partition sums are equal.
Even Zumkeller numbers are common; odd Zumkeller numbers are much less so. For values below 10^6, there is at least one Zumkeller number in every 12 consecutive integers, and the vast majority of them are even. The odd Zumkeller numbers are very similar to the list from the task Abundant odd numbers; they are nearly the same except for the further restriction that the abundance (A(n) = sigma(n) - 2n), must be even: A(n) mod 2 == 0
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Zumkeller numbers step by step in the zkl programming language
Source code in the zkl programming language
fcn properDivs(n){ // does not include n
// if(n==1) return(T); // we con't care about this case
( pd:=[1..(n).toFloat().sqrt()].filter('wrap(x){ n%x==0 }) )
.pump(pd,'wrap(pd){ if(pd!=1 and (y:=n/pd)!=pd ) y else Void.Skip })
}
fcn canSum(goal,divs){
if(goal==0 or divs[0]==goal) return(True);
if(divs.len()>1){
if(divs[0]>goal) return(canSum(goal,divs[1,*])); // tail recursion
else return(canSum(goal - divs[0], divs[1,*]) or canSum(goal, divs[1,*]));
}
False
}
fcn isZumkellerW(n){ // a filter for a iterator
ds,sum := properDivs(n), ds.sum(0) + n;
// if sum is odd, it can't be split into two partitions with equal sums
if(sum.isOdd) return(Void.Skip);
// if n is odd use 'abundant odd number' optimization
if(n.isOdd){
abundance:=sum - 2*n;
return( if(abundance>0 and abundance.isEven) n else Void.Skip);
}
canSum(sum/2,ds) and n or Void.Skip // sum is even
}
println("First 220 Zumkeller numbers:");
zw:=[2..].tweak(isZumkellerW);
do(11){ zw.walk(20).pump(String,"%4d ".fmt).println() }
println("\nFirst 40 odd Zumkeller numbers:");
zw:=[3..*, 2].tweak(isZumkellerW);
do(4){ zw.walk(10).pump(String,"%5d ".fmt).println() }
println("\nThe first 40 odd Zumkeller numbers which don't end in 5 are:");
zw:=[3..*, 2].tweak(fcn(n){ if(n%5) isZumkellerW(n) else Void.Skip });
do(5){ zw.walk(8).pump(String,"%7d ".fmt).println() }
You may also check:How to resolve the algorithm Perfect shuffle step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Permutations step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Python programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the Haskell programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the Julia programming language