Super Niven Numbers
Last Updated :
10 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
bool isDivBySubsetSums(vector< int > arr, int num)
{
int n = arr.size();
long long total = 1 << n;
for ( long long i = 0; i < total; i++) {
long long sum = 0;
for ( int j = 0; j < n; j++)
if (i & (1 << j))
sum += arr[j];
if (sum != 0 && num % sum != 0)
return false ;
}
return true ;
}
bool isSuperNivenNum( int n)
{
int temp = n;
vector< int > digits;
while (n != 0) {
int digit = n % 10;
digits.push_back(digit);
n = n / 10;
}
return isDivBySubsetSums(digits, temp);
}
int main()
{
int n = 500;
if (isSuperNivenNum(n))
cout << "yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isDivBySubsetSums(Vector<Integer> arr,
int num)
{
int n = arr.size();
long total = 1 << n;
for ( long i = 0 ; i < total; i++)
{
long sum = 0 ;
for ( int j = 0 ; j < n; j++)
if ((i & ( 1 << j)) > 0 )
sum += arr.get(j);
if (sum != 0 && num % sum != 0 )
return false ;
}
return true ;
}
static boolean isSuperNivenNum( int n)
{
int temp = n;
Vector<Integer> digits = new Vector<Integer>();
while (n != 0 )
{
int digit = n % 10 ;
digits.add(digit);
n = n / 10 ;
}
return isDivBySubsetSums(digits, temp);
}
public static void main(String[] args)
{
int n = 500 ;
if (isSuperNivenNum(n))
System.out.print( "yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isDivBySubsetSums(arr, num):
n = len (arr)
total = 1 << n
i = 0
while i < total:
sum = 0
j = 0
while j < n:
if (i & ( 1 << j)):
sum + = arr[j]
j + = 1
if ( sum ! = 0 ) and (num % sum ! = 0 ):
return False
i + = 1
return True
def isSuperNivenNum(n):
temp = n
digits = []
while (n > 1 ):
digit = int (n) % 10
digits.append(digit)
n = n / 10
return isDivBySubsetSums(digits, temp)
if __name__ = = '__main__' :
n = 500
if isSuperNivenNum(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool isDivBySubsetSums(List< int > arr,
int num)
{
int n = arr.Count;
long total = 1 << n;
for ( long i = 0; i < total; i++)
{
long sum = 0;
for ( int j = 0; j < n; j++)
if ((i & (1 << j)) > 0)
sum += arr[j];
if (sum != 0 && num % sum != 0)
return false ;
}
return true ;
}
static bool isSuperNivenNum( int n)
{
int temp = n;
List< int > digits = new List< int >();
while (n != 0)
{
int digit = n % 10;
digits.Add(digit);
n = n / 10;
}
return isDivBySubsetSums(digits, temp);
}
public static void Main(String[] args)
{
int n = 500;
if (isSuperNivenNum(n))
Console.Write( "yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isDivBySubsetSums(arr, num)
{
var n = arr.length;
var total = 1 << n;
for ( var i = 0; i < total; i++) {
var sum = 0;
for ( var j = 0; j < n; j++)
if (i & (1 << j))
sum += arr[j];
if (sum != 0 && num % sum != 0)
return false ;
}
return true ;
}
function isSuperNivenNum(n)
{
var temp = n;
var digits = [];
while (n != 0) {
var digit = n % 10;
digits.push(digit);
n = parseInt(n / 10);
}
return isDivBySubsetSums(digits, temp);
}
var n = 500;
if (isSuperNivenNum(n))
document.write( "yes" );
else
document.write( "No" );
</script>
|
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivBySubsetSums(vector< int > arr, int num)
{
int n = arr.size();
long long total = 1 << n;
for ( long long i = 0; i < total; i++) {
long long sum = 0;
for ( int j = 0; j < n; j++)
if (i & (1 << j))
sum += arr[j];
if (sum != 0 && num % sum != 0)
return false ;
}
return true ;
}
bool isSuperNivenNum( int n)
{
int temp = n;
vector< int > digits;
while (n != 0) {
int digit = n % 10;
digits.push_back(digit);
n = n / 10;
}
return isDivBySubsetSums(digits, temp);
}
int main()
{
int n = 500;
if (isSuperNivenNum(n))
cout << "yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isDivBySubsetSums(Vector<Integer> arr,
int num)
{
int n = arr.size();
long total = 1 << n;
for ( long i = 0 ; i < total; i++)
{
long sum = 0 ;
for ( int j = 0 ; j < n; j++)
if ((i & ( 1 << j)) > 0 )
sum += arr.get(j);
if (sum != 0 && num % sum != 0 )
return false ;
}
return true ;
}
static boolean isSuperNivenNum( int n)
{
int temp = n;
Vector<Integer> digits = new Vector<Integer>();
while (n != 0 )
{
int digit = n % 10 ;
digits.add(digit);
n = n / 10 ;
}
return isDivBySubsetSums(digits, temp);
}
public static void main(String[] args)
{
int n = 500 ;
if (isSuperNivenNum(n))
System.out.print( "yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isDivBySubsetSums(arr, num):
n = len (arr)
total = 1 << n
i = 0
while i < total:
sum = 0
j = 0
while j < n:
if (i & ( 1 << j)):
sum + = arr[j]
j + = 1
if ( sum ! = 0 ) and (num % sum ! = 0 ):
return False
i + = 1
return True
def isSuperNivenNum(n):
temp = n
digits = []
while (n > 1 ):
digit = int (n) % 10
digits.append(digit)
n = n / 10
return isDivBySubsetSums(digits, temp)
if __name__ = = '__main__' :
n = 500
if isSuperNivenNum(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool isDivBySubsetSums(List< int > arr,
int num)
{
int n = arr.Count;
long total = 1 << n;
for ( long i = 0; i < total; i++)
{
long sum = 0;
for ( int j = 0; j < n; j++)
if ((i & (1 << j)) > 0)
sum += arr[j];
if (sum != 0 && num % sum != 0)
return false ;
}
return true ;
}
static bool isSuperNivenNum( int n)
{
int temp = n;
List< int > digits = new List< int >();
while (n != 0)
{
int digit = n % 10;
digits.Add(digit);
n = n / 10;
}
return isDivBySubsetSums(digits, temp);
}
public static void Main(String[] args)
{
int n = 500;
if (isSuperNivenNum(n))
Console.Write( "yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isDivBySubsetSums(arr, num)
{
var n = arr.length;
var total = 1 << n;
for ( var i = 0; i < total; i++) {
var sum = 0;
for ( var j = 0; j < n; j++)
if (i & (1 << j))
sum += arr[j];
if (sum != 0 && num % sum != 0)
return false ;
}
return true ;
}
function isSuperNivenNum(n)
{
var temp = n;
var digits = [];
while (n != 0) {
var digit = n % 10;
digits.push(digit);
n = parseInt(n / 10);
}
return isDivBySubsetSums(digits, temp);
}
var n = 500;
if (isSuperNivenNum(n))
document.write( "yes" );
else
document.write( "No" );
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...