Panarithmic numbers within a given range | C ++ program to print Practical Numbers in given range ; function to compute divisors of a number ; vector to store divisors ; 1 will always be a divisor ; check if i is squareroot of A then only one time insert it in ans ; function to check that a number can be represented as sum of distinct divisor or not ; The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; If sum is 0 , then answer is true ; If sum is not 0 and set is empty , then answer is false ; Fill the subset table in bottom up manner ; return the possibility of given sum ; function to check a number is Practical or not ; vector to store divisors ; if all numbers can be represented as sum of unique divisors ; function to print Practical Numbers in a range ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > get_divisors ( int A ) { vector < int > ans ; ans . push_back ( 1 ) ; for ( int i = 2 ; i <= sqrt ( A ) ; i ++ ) { if ( A % i == 0 ) { ans . push_back ( i ) ; if ( ( i * i ) != A ) ans . push_back ( A / i ) ; } } return ans ; } bool Sum_Possible ( vector < int > set , int sum ) { int n = set . size ( ) ; bool subset [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j < set [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= set [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - set [ i - 1 ] ] ; } } return subset [ n ] [ sum ] ; } bool Is_Practical ( int A ) { vector < int > divisors ; divisors = get_divisors ( A ) ; for ( int i = 2 ; i < A ; i ++ ) { if ( Sum_Possible ( divisors , i ) == false ) return false ; } return true ; } void print_practica_No ( int A , int B ) { for ( int i = A ; i <= B ; i ++ ) { if ( Is_Practical ( i ) == true ) { cout << i << " ▁ " ; } } } int main ( ) { int A = 1 , B = 100 ; print_practica_No ( A , B ) ; return 0 ; }