Practical Number in Java

In this section, we will learn what is a practical number and also create Java programs to check if the given number is a practical number or not. The practical number program is frequently asked in Java coding interviews and academics.

Practical Number

A number X is said to be the practical number in Java if all the numbers Y that are less than X (Y < X) should be written as the addition of a unique proper divisor of X. Note that the proper divisor of a number excludes that number itself.

Steps to Find the Practical Numbers

Step 1: Assign a number to the variable.

Step 2: Find the proper divisors of the given number.

Step 3: Store these divisors in a list.

Step 4: Take all the numbers that are less than the given number (1 to given number - 1), one by one and try to find the subset from the list (found in step 3) whose sum is equal to the taken number.

Step 5: Check whether subsets are found or not for every number from 1 to given number - 1. If the subsets are found, then the given number is the practical number; otherwise, not.

Examples of Practical Number

Given, X = 10

Then, proper divisors of X are: 1, 2, 5

All the numbers that are less than 10 are Y = {1, 2, 3, 4, 5, 6, 7, 8, 9}

Check whether every number present in Y or not.

1 = 1 (1 is the proper divisor of X)

2 = 2 (2 is also the proper divisor of X)

3 = 1 + 2 (1 & 2 both are proper divisors of X. Also, they are unique)

4 = 2 + 2 (2 is the proper divisor of X. However, 2 has come twice, which is not unique)

Hence, we found at least one number which does not satisfy the stated condition. Therefore, the number 10 is not a practical number.

Given, X = 12

Then, proper divisors of X are: 1, 2, 3, 4, 6

All the numbers that are less than 12 are Y = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}

Check whether every number present in Y or not.

1 = 1 (1 is the proper divisor of X)

2 = 2 (2 is also the proper divisor of X)

3 = 1 + 2 (1 & 2 both are proper divisors of X. Also, they are unique)

4 = 1 + 3 (1 & 3 both are proper divisors of X. Also, they are unique)

5 = 2 + 3 (2 & 3 both are proper divisors of X. Also, they are unique)

6 = 2 + 4 (2 & 4 both are proper divisors of X. Also, they are unique)

7 = 3 + 4 (3 & 4 both are proper divisors of X. Also, they are unique)

8 = 2 + 6 (2 & 6 both are proper divisors of X. Also, they are unique)

9 = 3 + 6 (3 & 6 both are proper divisors of X. Also, they are unique)

10 = 1 + 3 + 6 (1, 3, & 6 both are proper divisors of X. Also, they are unique)

11 = 2 + 3 + 6 (2, 3 & 6 all are proper divisors of X. Also, they are unique)

Hence, we found that all the numbers present in Y satisfy the given condition. Therefore, the number 12 is a practical number.

Practical Number in Java

Practical Number Java Program

Observe the following Java program that checks the practical numbers from 1 to 20.

FileName: PracticalNumberExample.java

Output:

The number 1 is the practical number.
The number 2 is the practical number.
The number 3 is not the practical number.
The number 4 is the practical number.
The number 5 is not the practical number.
The number 6 is the practical number.
The number 7 is not the practical number.
The number 8 is the practical number.
The number 9 is not the practical number.
The number 10 is not the practical number.
The number 11 is not the practical number.
The number 12 is the practical number.
The number 13 is not the practical number.
The number 14 is not the practical number.
The number 15 is not the practical number.
The number 16 is the practical number.
The number 17 is not the practical number.
The number 18 is the practical number.
The number 19 is not the practical number.
The number 20 is the practical number.

Explanation: For every number from 1 to 20, the method isPracticalNo() is invoked with the help of for-loop. For every number, a vector factor is created for storing its divisors. Now, for every number from 1 to the given number (num), we try to find the subset with the help of dynamic programming. If for every number from 1 to the given number (num) we find a subset, then the given number (num) is the perfect number; otherwise, not.

Note: Instead of dynamic programming, one can also use recursion to find the subsets. However, the recursive approach will take more time to produce subsets as compared to the dynamic programming approach.