Números de Super Niven

Super Niven Number es un número N si es divisible no solo por la suma de sus dígitos sino también por la suma de cualquier subconjunto de sus dígitos (distintos de cero).
Por ejemplo:

68040 es un Número Super Niven porque es divisible por 6, 8, 4, 6+8, 6+4, 4+8 y 6+4+8.

Comprobar si N es un número de Super Niven

Dado un número N , la tarea es verificar si N es un Número Super Niven o no. Si N es un número Super Niven, escriba «Sí» , de lo contrario, escriba «No» .
Ejemplos:

Entrada: N = 68040 
Salida: Sí 
Explicación: 
68040 es divisible por 6, 8, 4, 6+8, 6+4, 4+8 y 6+4+8. 
y N comienza también con ’25’.
Entrada: N = 72 
Salida: No

Enfoque: :

  1. Almacenaremos todos los dígitos del Número N en una array arr
  2. Ahora encontraremos la suma de cada subconjunto de la array y verificaremos si el número N es divisible por todos los subconjuntos o no .
  3. Si N no es divisible por ninguno de los subconjuntos, devuelva falso; de lo contrario, devolverá verdadero al final.

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ implementation to check if a number
// is Super Niven Number or not.
 
#include <bits/stdc++.h>
using namespace std;
 
// Checks if sums of all subsets of digits array
// divides the number N
bool isDivBySubsetSums(vector<int> arr, int num)
{
    // to calculate length of array arr
    int n = arr.size();
 
    // There are total 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 representation 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<int> 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 implementation 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<Integer> arr,
                                             int num)
{
    // to calculate length of array arr
    int n = arr.size();
 
    // There are total 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 representation 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<Integer> digits = new Vector<Integer>();
 
    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 implementation 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 total 2^n subsets
    total = 1 << n
 
    # Consider all numbers from 0 to 2^n - 1
    i = 0
    while i < total:
        sum = 0
 
        # Consider binary representation 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# implementation 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<int> arr,
                                   int num)
{
    // to calculate length of array arr
    int n = arr.Count;
  
    // There are total 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 representation 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<int> digits = new List<int>();
  
    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

Javascript

<script>
 
// Javascript implementation to check if a number
// is Super Niven Number or not.
 
// Checks if sums of all subsets of digits array
// divides the number N
function isDivBySubsetSums(arr, num)
{
    // to calculate length of array arr
    var n = arr.length;
 
    // There are total 2^n subsets
    var total = 1 << n;
 
    // Consider all numbers from 0 to 2^n - 1
    for (var i = 0; i < total; i++) {
        var sum = 0;
 
        // Consider binary representation of
        // current i to decide which elements
        // to pick.
        for (var 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
function isSuperNivenNum(n)
{
    var temp = n;
    // to stor digits of N
    var digits = [];
 
    while (n != 0) {
        var digit = n % 10;
        digits.push(digit);
        n = parseInt(n / 10);
    }
 
    return isDivBySubsetSums(digits, temp);
}
 
// Driver code
var n = 500;
if (isSuperNivenNum(n))
    document.write("yes");
else
    document.write("No");
 
 
</script>
Producción: 

yes

Publicación traducida automáticamente

Artículo escrito por spp____ y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *