Skip to content
Related Articles

Related Articles

Find the Nth term of the Zumkeller Numbers

View Discussion
Improve Article
Save Article
  • Last Updated : 26 Sep, 2022
View Discussion
Improve Article
Save Article

Zumkeller numbers are the set of numbers whose divisors can be partitioned into two disjoint sets that sum to the same value. The first few zumkeller numbers are 6, 12, 20, 24, 28, 30, 40, 42, 48, 54, …
In this article, we will find the Nth Zumkeller number. 
Find the Nth Zumkeller Number: Given a number N, the task is to find the Nth Zumkeller number. 
Examples: 
 

Input: N = 2 
Output: 12 
Explanation: 
The second Zumkeller number is 12.
Input: N = 5 
Output: 28 
 

 

Approach: The following steps are followed to compute the answer. 
 

  1. Get the number N.
  2. Iterate over the loop starting from i = 1 until Nth Zumkeller number is found.
  3. Check the number ‘i’ is a zumkeller number or not.
  4. If yes, then repeat the above step for i+1 and increment the counter.
  5. If no, then repeat the above step for i+1 without incrementing the counter.
  6. Finally, when the counter is equal to the number N, that number is the N-th zumkeller number. Print the value of ‘i’ and break the loop.

Below is the implementation of the above approach:
 

Python3




# Python program to find the
# N-th Zumkeller number
 
# Function to find all the
# divisors
def Divisors(n) :
    l = []
    i = 1
    while i <= n :
        if (n % i == 0) :
            l.append(i)
        i = i + 1
    return l
 
# Function to check if the sum
# of the subset of divisors is
# equal to sum / 2 or not
def PowerSet(arr, n, s):
        
    # List to find all the
    # subsets of the given set.
    # Any repeated subset is
    # considered only 
    # once in the output
    _list = []
    
    # Run a counter i 
    for i in range(2**n):
        subset = ""
    
        # Consider each element
        # in the set
        for j in range(n):
    
            # Check if j-th bit in
            # the i is set. 
            # If the bit is set,
            # we consider 
            # j-th element from set
            if (i & (1 << j)) != 0:
                subset += str(arr[j]) + "|"
    
        # Check if the subset is
        # encountered for the first time.
        if subset not in _list and len(subset) > 0:
            _list.append(subset)
    
    # Consider every subset
    for subset in _list:
        sum = 0
 
        # Split the subset and
        # sum of its elements
        arr = subset.split('|')
        for string in arr[:-1]:
            sum += int(string)
 
            # If the sum is equal
            # to S
            if sum == s:
                return True
 
    return False
 
# Function to check if a number
# is a Zumkeller number
def isZumkeller(n):
 
    # To find all the divisors
    # of a number N
    d = Divisors(n)
 
    # Finding the sum of
    # all the divisors
    s = sum(d)
 
    # Check for the condition that
    # sum must be even and the
    # maximum divisor is less than
    # or equal to sum / 2.
    # If the sum is odd and the
    # maximum divisor is greater than
    # sum / 2, then it is not possible
    # to divide the divisors into
    # two sets
    if not s % 2 and max(d) <= s / 2:
 
        # For all the subsets of
        # the divisors
        if PowerSet(d, len(d), s / 2) :
                return True
 
    return False
 
  
# Function to print N-th
# Zumkeller number
def printZumkellers(N):
    val = 0
    ans = 0
 
    # Iterating through all
    # the numbers
    for n in range(1, 10**5):
  
        # Check if n is a
        # Zumkeller number
        if isZumkeller(n):
            ans = n
            val += 1
 
            # Check if N-th Zumkeller number
            # is obtained or not
            if val >= N:
                break              
 
    print(ans)
  
# Driver code
if __name__ == '__main__':
  
    N = 4
  
    printZumkellers(N)

Javascript




// Python program to find the
// N-th Zumkeller number
 
// Function to find all the
// divisors
function Divisors(n)
{
    let l = []
    let i = 1
    while (i <= n)
    {
        if (n % i == 0)
            l.push(i)
        i = i + 1
    }
    return l
}
 
// Function to check if the sum
// of the subset of divisors is
// equal to sum / 2 or not
function PowerSet(arr, n, s)
{
        
    // List to find all the
    // subsets of the given set.
    // Any repeated subset is
    // considered only 
    // once in the output
    let _list = []
    
    // Run a counter i 
    for (var i = 0; i < 2 ** n; i++)
    {
        let subset = ""
    
        // Consider each element
        // in the set
        for (var j = 0; j < n; j++)
        {
    
            // Check if j-th bit in
            // the i is set. 
            // If the bit is set,
            // we consider 
            // j-th element from set
            if ((i & (1 << j)) != 0)
                subset += ((arr[j]) + "|")
        }
    
        // Check if the subset is
        // encountered for the first time.
        if (!_list.includes(subset) && subset.length > 0)
            _list.push(subset)
    }
    
    // Consider every subset
    for (let subset of _list)
    {
        let sum = 0
 
        // Split the subset and
        // sum of its elements
        let arr = subset.split('|')
        arr.pop()
        for (let string of arr)
        {
            sum += parseInt(string)
 
            // If the sum is equal
            // to S
            if (sum == s)
                return true
        }
    }
    return false
}
 
// Function to check if a number
// is a Zumkeller number
function isZumkeller(n)
{
 
    // To find all the divisors
    // of a number N
    let d = Divisors(n)
 
    // Finding the sum of
    // all the divisors
    let s = 0;
    for (let d1 of d)
        s += d1;
 
 
    // Check for the condition that
    // sum must be even and the
    // maximum divisor is less than
    // or equal to sum / 2.
    // If the sum is odd and the
    // maximum divisor is greater than
    // sum / 2, then it is not possible
    // to divide the divisors into
    // two sets
    let maxd = Math.max(...d)
    if (s % 2 == 0  && maxd <= (s / 2) )
 
        // For all the subsets of
        // the divisors
        if (PowerSet(d, d.length, s / 2))
                return true
 
    return false
}
 
// Function to print N-th
// Zumkeller number
function printZumkellers(N)
{
    let val = 0
    let ans = 0
 
    // Iterating through all
    // the numbers
    for (var n = 1; n < 10 ** 5; n ++)
    {
  
        // Check if n is a
        // Zumkeller number
        if (isZumkeller(n))
        {
            ans = n
            val += 1
 
            // Check if N-th Zumkeller number
            // is obtained or not
            if (val >= N)
                break   
        }
    }
 
    console.log(ans)
}
  
// Driver code
let N = 4
  
printZumkellers(N)
 
// This code is contributed by phasing17

Output: 

24

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!