Zumkeller数字是其除数可以划分为两个不相交的集合的总和,这些集合的总和等于相同的值。前几个Zumkeller编号是6、12、20、24、28、30、40、42、48、54 … 。
在本文中,我们将找到第N个Zumkeller编号。
查找第N个Zumkeller编号:给定数字N ,任务是找到第N个Zumkeller编号。
例子:
Input: N = 2
Output: 12
Explanation:
The second Zumkeller number is 12.
Input: N = 5
Output: 28
方法:按照以下步骤计算答案。
- 获取数字N。
- 从i = 1开始循环遍历,直到找到第N个Zumkeller编号。
- 检查数字“ i”是否为zumkeller数字。
- 如果是,则对i + 1重复上述步骤并增加计数器。
- 如果否,则对i + 1重复上述步骤,而无需增加计数器。
- 最后,当计数器等于数字N时,该数字即为第N个祖克梅勒数字。打印’i’的值并中断循环。
下面是上述方法的实现:
Python3
# Python program to find the
# N-th Zumkeller number
# Function to find all the
# divisiors
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)
输出:
24