📜  超级尼文数字

📅  最后修改于: 2021-05-04 23:48:04             🧑  作者: Mango

如果超级Niven Number不仅可以被其数字的总和而且可以被其(非零)数字的任何子集的总和整除,则它是数字N。
例如:

检查N是否为超级数

给定数字N ,任务是检查N是否为超级尼文数。如果N是超级尼文数,则打印“是”,否则打印“否”
例子:

方法

  1. 我们将数字N的所有数字存储在数组arr中
  2. 现在我们将找到数组每个子集的总和,并检查数字N是否可被所有子集整除
  3. 如果N无法被任何子集整除,则返回false,否则最后返回true。

下面是上述方法的实现:

C++
// C++ implementagtion to check if a number
// is Super Niven Number or not.
  
#include 
using namespace std;
  
// Checks if sums of all subsets of digits array
// divides the number N
bool isDivBySubsetSums(vector arr, int num)
{
    // to calculate length of array arr
    int n = arr.size();
  
    // There are totoal 2^n subsets
    long long total = 1 << n;
  
    // Consider all numbers from 0 to 2^n - 1
    for (long long i = 0; i < total; i++) {
        long long sum = 0;
  
        // Consider binary reprsentation of
        // current i to decide which elements
        // to pick.
        for (int j = 0; j < n; j++)
            if (i & (1 << j))
                sum += arr[j];
  
        // check sum of picked elements.
        if (sum != 0 && num % sum != 0)
            return false;
    }
    return true;
}
  
// Function to check if a number is 
// a super-niven number
bool isSuperNivenNum(int n)
{
    int temp = n;
    // to stor digits of N
    vector digits;
  
    while (n != 0) {
        int digit = n % 10;
        digits.push_back(digit);
        n = n / 10;
    }
  
    return isDivBySubsetSums(digits, temp);
}
  
// Driver code
int main()
{
    int n = 500;
    if (isSuperNivenNum(n))
        cout << "yes";
    else
        cout << "No";
    return 0;
}


Java
// Java implementagtion to check if a number
// is Super Niven Number or not.
import java.util.*;
class GFG{
  
// Checks if sums of all subsets of digits array
// divides the number N
static boolean isDivBySubsetSums(Vector arr, 
                                             int num)
{
    // to calculate length of array arr
    int n = arr.size();
  
    // There are totoal 2^n subsets
    long total = 1 << n;
  
    // Consider all numbers from 0 to 2^n - 1
    for (long i = 0; i < total; i++) 
    {
        long sum = 0;
  
        // Consider binary reprsentation of
        // current i to decide which elements
        // to pick.
        for (int j = 0; j < n; j++)
            if ((i & (1 << j)) > 0)
                sum += arr.get(j);
  
        // check sum of picked elements.
        if (sum != 0 && num % sum != 0)
            return false;
    }
    return true;
}
  
// Function to check if a number is 
// a super-niven number
static boolean isSuperNivenNum(int n)
{
    int temp = n;
    // to stor digits of N
    Vector digits = new Vector();
  
    while (n != 0)
    {
        int digit = n % 10;
        digits.add(digit);
        n = n / 10;
    }
  
    return isDivBySubsetSums(digits, temp);
}
  
// Driver code
public static void main(String[] args)
{
    int n = 500;
    if (isSuperNivenNum(n))
        System.out.print("yes");
    else
        System.out.print("No");
}
}
  
// This code is contributed by Amit Katiyar


Python3
# Python3 implementagtion to check if a 
# number is Super Niven Number or not.
  
# Checks if sums of all subsets of digits 
# array divides the number N
def isDivBySubsetSums(arr, num):
  
    # To calculate length of array arr
    n = len(arr)
  
    # There are totoal 2^n subsets
    total = 1 << n
  
    # Consider all numbers from 0 to 2^n - 1
    i = 0
    while i < total:
        sum = 0
  
        # Consider binary reprsentation of
        # current i to decide which elements
        # to pick.
        j = 0
        while j < n:
            if (i & (1 << j)):
                sum += arr[j]
                  
            j += 1
  
        # Check sum of picked elements.
        if (sum != 0) and (num % sum != 0):
            return False
              
        i += 1
          
    return True
  
# Function to check if a number is 
# a super-niven number
def isSuperNivenNum(n):
  
    temp = n
      
    # To store digits of N
    digits = []
      
    while (n > 1):
        digit = int(n) % 10
        digits.append(digit)
        n = n / 10
  
    return isDivBySubsetSums(digits, temp)
  
# Driver code
if __name__ == '__main__': 
  
    n = 500
      
    if isSuperNivenNum(n):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by jana_sayantan


C#
// C# implementagtion to check if a number
// is Super Niven Number or not.
using System;
using System.Collections.Generic;
  
class GFG{
   
// Checks if sums of all subsets of digits array
// divides the number N
static bool isDivBySubsetSums(List arr, 
                                   int num)
{
    // to calculate length of array arr
    int n = arr.Count;
   
    // There are totoal 2^n subsets
    long total = 1 << n;
   
    // Consider all numbers from 0 to 2^n - 1
    for (long i = 0; i < total; i++) 
    {
        long sum = 0;
   
        // Consider binary reprsentation of
        // current i to decide which elements
        // to pick.
        for (int j = 0; j < n; j++)
            if ((i & (1 << j)) > 0)
                sum += arr[j];
   
        // check sum of picked elements.
        if (sum != 0 && num % sum != 0)
            return false;
    }
    return true;
}
   
// Function to check if a number is 
// a super-niven number
static bool isSuperNivenNum(int n)
{
    int temp = n;
    // to stor digits of N
    List digits = new List();
   
    while (n != 0)
    {
        int digit = n % 10;
        digits.Add(digit);
        n = n / 10;
    }
    return isDivBySubsetSums(digits, temp);
}
   
// Driver code
public static void Main(String[] args)
{
    int n = 500;
    if (isSuperNivenNum(n))
        Console.Write("yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by gauravrajput1


输出:
yes

参考:http://www.numbersaplenty.com/set/super_Niven_number/