How to resolve the algorithm Zumkeller numbers step by step in the D programming language
How to resolve the algorithm Zumkeller numbers step by step in the D 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 D programming language
Source code in the d programming language
import std.algorithm;
import std.stdio;
int[] getDivisors(int n) {
auto divs = [1, n];
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
divs ~= i;
int j = n / i;
if (i != j) {
divs ~= j;
}
}
}
return divs;
}
bool isPartSum(int[] divs, int sum) {
if (sum == 0) {
return true;
}
auto le = divs.length;
if (le == 0) {
return false;
}
auto last = divs[$ - 1];
int[] newDivs;
for (int i = 0; i < le - 1; i++) {
newDivs ~= divs[i];
}
if (last > sum) {
return isPartSum(newDivs, sum);
} else {
return isPartSum(newDivs, sum) || isPartSum(newDivs, sum - last);
}
}
bool isZumkeller(int n) {
auto divs = getDivisors(n);
auto sum = divs.sum();
// if sum is odd can't be split into two partitions with equal sums
if (sum % 2 == 1) {
return false;
}
// if n is odd use 'abundant odd number' optimization
if (n % 2 == 1) {
auto abundance = sum - 2 * n;
return abundance > 0 && abundance % 2 == 0;
}
// if n and sum are both even check if there's a partition which totals sum / 2
return isPartSum(divs, sum / 2);
}
void main() {
writeln("The first 220 Zumkeller numbers are:");
int i = 2;
for (int count = 0; count < 220; i++) {
if (isZumkeller(i)) {
writef("%3d ", i);
count++;
if (count % 20 == 0) {
writeln;
}
}
}
writeln("\nThe first 40 odd Zumkeller numbers are:");
i = 3;
for (int count = 0; count < 40; i += 2) {
if (isZumkeller(i)) {
writef("%5d ", i);
count++;
if (count % 10 == 0) {
writeln;
}
}
}
writeln("\nThe first 40 odd Zumkeller numbers which don't end in 5 are:");
i = 3;
for (int count = 0; count < 40; i += 2) {
if (i % 10 != 5 && isZumkeller(i)) {
writef("%7d ", i);
count++;
if (count % 8 == 0) {
writeln;
}
}
}
}
You may also check:How to resolve the algorithm Execute Computer/Zero step by step in the Nim programming language
You may also check:How to resolve the algorithm Statistics/Normal distribution step by step in the Maple programming language
You may also check:How to resolve the algorithm Program termination step by step in the C# programming language
You may also check:How to resolve the algorithm Best shuffle step by step in the Scala programming language
You may also check:How to resolve the algorithm Diversity prediction theorem step by step in the Racket programming language