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

Published on 12 May 2024 09:40 PM

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

Source code in the factor programming language

USING: combinators grouping io kernel lists lists.lazy math
math.primes.factors memoize prettyprint sequences ;

MEMO: psum? ( seq n -- ? )
    {
        { [ dup zero? ] [ 2drop t ] }
        { [ over length zero? ] [ 2drop f ] }
        { [ over last over > ] [ [ but-last ] dip psum? ] }
        [
            [ [ but-last ] dip psum? ]
            [ over last - [ but-last ] dip psum? ] 2bi or
        ]
    } cond ;

: zumkeller? ( n -- ? )
    dup divisors dup sum
    {
        { [ dup odd? ] [ 3drop f ] }
        { [ pick odd? ] [ nip swap 2 * - [ 0 > ] [ even? ] bi and ] }
        [ nipd 2/ psum? ]
    } cond ;

: zumkellers ( -- list )
    1 lfrom [ zumkeller? ] lfilter ;

: odd-zumkellers ( -- list )
    1 [ 2 + ] lfrom-by [ zumkeller? ] lfilter ;

: odd-zumkellers-no-5 ( -- list )
    odd-zumkellers [ 5 mod zero? not ] lfilter ;

: show ( count list row-len -- )
    [ ltake list>array ] dip group simple-table. nl ;

"First 220 Zumkeller numbers:" print
220 zumkellers 20 show

"First 40 odd Zumkeller numbers:" print
40 odd-zumkellers 10 show

"First 40 odd Zumkeller numbers not ending with 5:" print
40 odd-zumkellers-no-5 8 show


  

You may also check:How to resolve the algorithm Matrix transposition step by step in the Fermat programming language
You may also check:How to resolve the algorithm Periodic table step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Anti-primes step by step in the Sidef programming language
You may also check:How to resolve the algorithm Metaprogramming step by step in the Erlang programming language
You may also check:How to resolve the algorithm Galton box animation step by step in the Icon and Unicon programming language