Super Niven Number is a number N if it is divisible not only by the sum of its digits but also by the sum of any subset of its (nonzero) digits.
For example:
68040 is a Super Niven Number because it is divisible by 6, 8, 4, 6+8, 6+4, 4+8 and 6+4+8.
Check if N is a Super Niven number
Given a number N, the task is to check if N is a Super Niven Number or not. If N is a Super Niven Number then print “Yes” else print “No”.
Examples:
Input: N = 68040
Output: Yes
Explanation:
68040 is divisible by 6, 8, 4, 6+8, 6+4, 4+8 and 6+4+8.
and N begins also with ’25’.
Input: N = 72
Output: No
Approach: :
- We will store all the digits of the Number N in an array arr
- Now We will find the sum of every subset of the array and check if the number N is divisible by all subset or not
- If N is not divisible by any of the subsets then return false else return true at last.
Below is the implementation of the above approach:
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 Nbool 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 numberbool isSuperNivenNum(int n){ int temp = n; // to store 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 codeint 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 Nstatic 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 numberstatic boolean isSuperNivenNum(int n){ int temp = n; // to store 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 codepublic 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 Ndef 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 numberdef 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 codeif __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 Nstatic 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 numberstatic bool isSuperNivenNum(int n){ int temp = n; // to store 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 codepublic 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 Nfunction 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 numberfunction isSuperNivenNum(n){ var temp = n; // to store digits of N var digits = []; while (n != 0) { var digit = n % 10; digits.push(digit); n = parseInt(n / 10); } return isDivBySubsetSums(digits, temp);}// Driver codevar n = 500;if (isSuperNivenNum(n)) document.write("yes");else document.write("No");</script> |
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 Nbool 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 numberbool isSuperNivenNum(int n){ int temp = n; // to store 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 codeint 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 Nstatic 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 numberstatic 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 codepublic 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 Ndef 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 numberdef 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 codeif __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 Nstatic 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 numberstatic 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 codepublic 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 Nfunction 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 numberfunction 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 codevar n = 500;if (isSuperNivenNum(n)) document.write("yes");else document.write("No");</script> |
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Read More to that Topic: geeksforgeeks.org/super-niven-numbers/ […]