Open In App

Find the Nth term of the Zumkeller Numbers

Improve
Improve
Like Article
Like
Save
Share
Report

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:
 

C++




#include<bits/stdc++.h>
using namespace std;
// C++ program to find the
// N-th Zumkeller number
 
// Function to find all the
// divisors
vector<unsigned long long> Divisors(unsigned long long n)
{
    vector<unsigned long long> l;
    unsigned long long i = 1;
 
    while (i <= n)
    {
        if (n % i == 0)
            l.push_back(i);
        i = i + 1;
    }
    return l;
}
 
// Function to check if the sum
// of the subset of divisors is
// equal to sum / 2 or not
bool PowerSet(vector<unsigned long long> arr,unsigned long long n,unsigned long long s)
{
        
    // List to find all the
    // subsets of the given set.
    // Any repeated subset is
    // considered only 
    // once in the output
    vector<vector<unsigned long long>> _list;
    
    // Run a counter i 
    for (int i = 0; i < pow(2, n); i++)
    {
        vector<unsigned long long> subset;
    
         
        // Consider each element
        // in the set
        for (int 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.push_back(arr[j]);
        }
    
        // Check if the subset is
        // encountered for the first time.
        auto it = find(_list.begin(), _list.end(), subset);
        if (it != _list.end() && subset.size() > 0)
            _list.push_back(subset);
    }
    
    // Consider every subset
    for (auto subset: _list)
    {
        int sum = 0;
 
        // Split the subset and
        // sum of its elements
        for (auto num: subset)
        {
            sum += num;
 
            // If the sum is equal
            // to S
            if (sum == s)
                return true;
        }
    }
    return false;
}
 
// Function to check if a number
// is a Zumkeller number
bool isZumkeller(unsigned long long n)
{
 
    // To find all the divisors
    // of a number N
    vector<unsigned long long> d = Divisors(n);
    unsigned long long maxd = -1000;
    // Finding the sum of
    // all the divisors
    unsigned long long s = 0;
    for (auto d1: d){
        s += d1;
        maxd = max(maxd, 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
    if (s % 2 == 0  && maxd <= (s / 2) )
 
        // For all the subsets of
        // the divisors
        if (PowerSet(d, d.size(), s / 2))
                return true;
 
    return false;
}
 
// Function to print N-th
// Zumkeller number
void Zumkellers(unsigned long long N)
{
    unsigned long long val = 0;
    unsigned long long ans = 0;
 
    // Iterating through all
    // the numbers
    for (int n = 1; n < 100000; 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
        }
    }
    cout << ans << endl;
}
  
// Driver code
int main(){
    unsigned long long N = 4;
    Zumkellers(N);
    return 0;
     
}
 
// This code is contributed by Nidhi goel.


Java




import java.util.*;
 
public class Main {
 
    // Function for finding divisors
    static ArrayList<Integer> divisors(int n) {
        ArrayList<Integer> l = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            if (n % i == 0) {
                l.add(i);
            }
        }
        return l;
    }
 
    // Function to check if the sum of the subset of divisors is equal to sum / 2 or not
    static boolean powerSet(int[] arr, int n, int s) {
        ArrayList<String> list = new ArrayList<>();
 
        // Run a counter i
        for (int i = 0; i < (1 << n); i++) {
            StringBuilder subset = new StringBuilder();
 
            // Consider each element in the set
            for (int 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.append(arr[j]).append("|");
                }
            }
 
            // Check if the subset is encountered for the first time.
            if (!list.contains(subset.toString()) && subset.length() > 0) {
                list.add(subset.toString());
            }
        }
 
        // Consider every subset
        for (String subset : list) {
            int sum = 0;
 
            // Split the subset and sum of its elements
            String[] strArr = subset.split("\\|");
            for (String string : strArr) {
                sum += Integer.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
    static boolean isZumkeller(int n) {
 
        // To find all the divisors of a number N
        ArrayList<Integer> d = divisors(n);
 
        // Finding the sum of all the divisors
        int s = 0;
        for (int i : d) {
            s += i;
        }
 
        // 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 (s % 2 == 0 && Collections.max(d) <= s / 2) {
 
            // For all the subsets of the divisors
            if (powerSet(d.stream().mapToInt(Integer::intValue).toArray(), d.size(), s / 2)) {
                return true;
            }
        }
 
        return false;
    }
 
    // Function to print N-th Zumkeller number
    static void printZumkellers(int n) {
        int val = 0;
        int ans = 0;
 
        // Iterating through all the numbers
        for (int i = 1; i <= 100000; i++) {
 
            // Check if i is a Zumkeller number
            if (isZumkeller(i)) {
                ans = i;
                val++;
 
                // Check if N-th Zumkeller number is obtained or not
                if (val >= n) {
                    break;
                }
            }
        }
 
        System.out.println(ans);
    }
 
   // Driver code
public static void main(String[] args) {
int N = 4;
printZumkellers(N);
}}


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)


C#




using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG
{
 
  // Function for finding divisors
  static List<int> Divisors(int n)
  {
    List<int> l = new List<int>();
    for (int i = 1; i <= n; i++) {
      if (n % i == 0) {
        l.Add(i);
      }
    }
    return l;
  }
 
  // Function to check if the sum of the subset of
  // divisors is equal to sum / 2 or not
  static bool PowerSet(int[] arr, int n, int s)
  {
    List<string> list = new List<string>();
 
    // Run a counter i
    for (int i = 0; i < (1 << n); i++) {
      System.Text.StringBuilder subset
        = new System.Text.StringBuilder();
 
      // Consider each element in the set
      for (int 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.Append(arr[j]).Append("|");
        }
      }
 
      // Check if the subset is encountered for the
      // first time.
      if (!list.Contains(subset.ToString())
          && subset.Length > 0) {
        list.Add(subset.ToString());
      }
    }
 
    // Consider every subset
    foreach(string subset in list)
    {
      int sum = 0;
 
      // Split the subset and sum of its elements
      string[] strArr = subset.Split('|');
      foreach(string str in strArr)
      {
        if (str.Length > 0)
          sum += int.Parse(str);
      }
 
      // If the sum is equal to S
      if (sum == s) {
        return true;
      }
    }
 
    return false;
  }
 
  // Function to check if a number is a Zumkeller number
  static bool IsZumkeller(int n)
  {
 
    // To find all the divisors of a number N
    List<int> d = Divisors(n);
 
    // Finding the sum of all the divisors
    int s = 0;
    foreach(int i in d) { s += i; }
 
    // 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 (s % 2 == 0 && d.Max() <= s / 2) {
 
      // For all the subsets of the divisors
      if (PowerSet(d.ToArray(), d.Count(), s / 2)) {
        return true;
      }
    }
 
    return false;
  }
 
  // Function to print N-th Zumkeller number
  static void PrintZumkellers(int n)
  {
    int val = 0;
    int ans = 0;
 
    // Iterating through all the numbers
    for (int i = 1; i <= 100000; i++) {
 
      // Check if i is a Zumkeller number
      if (IsZumkeller(i)) {
        ans = i;
        val++;
 
        // Check if N-th Zumkeller number is
        // obtained or not
        if (val >= n) {
          break;
        }
      }
    }
 
    Console.WriteLine(ans);
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    int 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

 



Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads