Panarithmic numbers within a given range | Java 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 Code
import java . util . * ; import java . math . * ; class GFG { static ArrayList < Integer > get_divisors ( int A ) { ArrayList < Integer > ans = new ArrayList < > ( ) ; ans . add ( 1 ) ; for ( int i = 2 ; i <= Math . sqrt ( A ) ; i ++ ) { if ( A % i == 0 ) { ans . add ( i ) ; if ( ( i * i ) != A ) ans . add ( A / i ) ; } } return ans ; } static boolean Sum_Possible ( ArrayList < Integer > set , int sum ) { int n = set . size ( ) ; boolean subset [ ] [ ] = new boolean [ 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 . get ( i - 1 ) ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= set . get ( i - 1 ) ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - set . get ( i - 1 ) ] ; } } return subset [ n ] [ sum ] ; } static boolean Is_Practical ( int A ) { ArrayList < Integer > divisors ; divisors = get_divisors ( A ) ; for ( int i = 2 ; i < A ; i ++ ) { if ( Sum_Possible ( divisors , i ) == false ) return false ; } return true ; } static void print_practica_No ( int A , int B ) { for ( int i = A ; i <= B ; i ++ ) { if ( Is_Practical ( i ) == true ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String args [ ] ) { int A = 1 , B = 100 ; print_practica_No ( A , B ) ; } }