How to resolve the algorithm Zumkeller numbers step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Zumkeller numbers step by step in the Wren 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 Wren programming language

Source code in the wren programming language

import "/math" for Int, Nums
import "/fmt" for Fmt
import "io" for Stdout

var isPartSum // recursive
isPartSum = Fn.new { |divs, sum|
    if (sum == 0) return true
    if (divs.count == 0) return false
    var last = divs[-1]
    divs = divs[0...-1]
    if (last > sum) return isPartSum.call(divs, sum)
    return isPartSum.call(divs, sum-last) || isPartSum.call(divs, sum)
}

var isZumkeller = Fn.new { |n|
    var divs = Int.divisors(n)
    var sum = Nums.sum(divs)
    // 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) {
        var 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.call(divs, sum / 2)
}

System.print("The first 220 Zumkeller numbers are:")
var count = 0
var i = 2
while (count < 220) {
    if (isZumkeller.call(i)) {
        Fmt.write("$3d ", i)
        Stdout.flush()
        count = count + 1
        if (count % 20 ==  0) System.print()
    }
    i = i + 1
}

System.print("\nThe first 40 odd Zumkeller numbers are:")
count = 0
i = 3
while (count < 40) {
    if (isZumkeller.call(i)) {
        Fmt.write("$5d ", i)
        Stdout.flush()
        count = count + 1
        if (count % 10 == 0) System.print()
    }
    i = i + 2
}

System.print("\nThe first 40 odd Zumkeller numbers which don't end in 5 are:")
count = 0
i = 3
while (count < 40) {
    if ((i % 10 != 5) && isZumkeller.call(i)) {
        Fmt.write("$7d ", i)
        Stdout.flush()
        count = count + 1
        if (count % 8 == 0) System.print()
    }
    i = i + 2
}
System.print()

  

You may also check:How to resolve the algorithm 2048 step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the ERRE programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the APL programming language
You may also check:How to resolve the algorithm Honaker primes step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Globally replace text in several files step by step in the Tcl programming language