×

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers are among the top listed coding interview questions.

Any number A is known as the practical number in Java if all the numbers B that are less than A (B<A) are written as the summation of a unique proper divisor of A.

It is noted that the proper divisor of a number does not include that number itself.

Steps to acquire the Practical Numbers

Step 1: Allocateor assign a number to the variable.

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

Step 3: Use a list to store these divisors.

Step 4: Take all the numbers that are less than the given number (range - 1 to n- 1 (n= given no.)), 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 if subsets are found or not for every number from 1 to n - 1. If the subsets are found, then the given number is the practical number; otherwise, not.

Illustrations

Let us now look at some examples to understand the concept of practical numbers in a better way.

Example 1

Given, that A = 10

Then, the proper divisors of A are: 1, 2, 5

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

Check if every number present in B 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.

Example 2

Given, that A = 15

Then, the proper divisors of A are: 1, 3, 5

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

Check whether every number is present in B or not.

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

2 = 2 (2 is not the proper divisor of A)

3 = 3 (3 is also the proper divisor of A)

4 = 2+2 (neither unique nor proper divisors of A

5 = 5 (5 is also the proper divisor of A)

Hence, we found all the numbers do not satisfy the condition required for practical numbers. Thus, 15 is not a practical number.

Example 3

Given, that A = 16

Then, the proper divisors of A are: 1, 2,4, 8

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

Check whether every number is present in B or not.

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

2 = 2 (2 is the proper divisor of A)

3 = 2+1(both are unique and the proper divisor of A)

4 = 3+1 (both are unique and the proper divisor of A)

5 = 3+2 (both are unique and the proper divisor of A)

6 = 4+2 (both are unique and the proper divisor of A)

7 = 3+4 (both are unique and the proper divisor of A)

8 = 7+1 (both are unique and the proper divisor of A)

9 = 8+1 (both are unique and the proper divisor of A)

10 = 9+1 (both are unique and the proper divisor of A)

11 = 10+1 (both are unique and the proper divisor of A)

12 = 11+1 (both are unique and the proper divisor of A)

13 = 12+1 (both are unique and the proper divisor of A)

Hence, we found all the numbers satisfy the condition required for practical numbers. Thus, 16 is a practical number.

Example 4

Given, that A = 6

Then, the proper divisors of A are: 1, 2, 3

All the numbers that are less than 15 are B = {1, 2, 3, 4, 5}

Check whether every number is present in B or not.

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

2 = 2 (2 is not the proper divisor of A)

3 = 3 (3 is also the proper divisor of A)

4 = 3+1 (both are unique and also the proper divisors of A)

5 = 3+2 (both are unique and also the proper divisors of A)

Hence, we found all the numbers do not satisfy the condition required for practical numbers. Thus, 6 is a practical number.

Implementation

Let us now look at a code in JAVA to understand the practical nature of the numbers starting from 1 and ending at 20.

// Online Java Compiler
// Import Statement  
import java.util.*;  


public class PracticalNumberEg
{  
// A method returns boolean value true whenever s is equal to the sum of a subset of v  
public booleanisAddSubset(int s, int size, Vector<Integer> v)  
{  
// The value of isSubset[j][i] will give out true, when  
// there is a subset of v[0..i-1] with the sum  
// equal to j   
booleanisSubset[][] = new boolean[size + 1][s + 1];  


// the answer is true whenever we get the sum as 0  
for (int j = 0; j <= size; j++)  
{  
isSubset[j][0] = true;  
}  


// the subset is not empty and the sum is 0. Hence, the  value  is false
for (int j = 1; j <= s; j++)  
{  
isSubset[0][j] = false;  
}  


// Filling the subset table in a bottom-up manner  
for (int j = 1; j <= size; j++)  
{  
for (int i = 1; i<= s; i++)  
{  
if (i<v.get(j - 1))  
{  
isSubset[j][i] = isSubset[j - 1][i];  
}  
if (i>= v.get(j - 1))  
{  
isSubset[j][i] = isSubset[j - 1][i] || isSubset[j - 1][i - v.get(j - 1)];  
}  
}  
}  
return isSubset[size][s];  
}  


// a method to keep or store all the divisors of no in the vector f  
public void keepDivisors(int no, Vector<Integer> f)  
{  
// A loop to keep track of all divisors that divides 'num'  
for (int j = 1; j <= Math.sqrt(no); j++)  
{  


// if 'j' is the divisor of 'no'  
if (no % j == 0)  
{  


// if divisor and quotient both are the same   
// then either divisor or quotient will be considered  
// for example - 9 / 3 = 3. In this case, quotient and   
// divisor both are 3. Hence, only one 3 will be stored in the vector f  
if (j == (no / j))  
f.add(j);  
else  
{  
f.add(j);  
f.add(no / j);  
}  
}  
}  
}  


// A method that returns true whenever a num is a practical number  
public booleanisPracticalNo(int num)  
{  
// a vector for storing all the factors of num
Vector<Integer> factor = new Vector<Integer>();  


// a method that fills the vector factor  
keepDivisors(num, factor);  
int size = factor.size();  


// to checking all numbers from 1 to <num
for (int j = 1; j <num; j++)  
{  
if (!isAddSubset(j, size, factor))  
return false;  
}  
return true;  
}  


// main method  
public static void main(String argvs[])  
{  
// creating an object of the class PracticalNumberExample
PracticalNumberEgpNumObj = new PracticalNumberEg();   




for(int i = 1; i<= 20; i++)  
{  
if(pNumObj.isPracticalNo(i) == true)  
System.out.println("The number " + i + " is a practical number.");  
else  
System.out.println("The number " + i + " is not a practical number.");  
}  
}  
}  

Output:

Practical Number in Java

Explanation: A loop runs from 1 to 20 and it is checked whether the numbers are practical numbers or not according to the devised code. To check if the no. is a practical number or not, the function named isPracticalNo is being called. A suitable message gets printed after checking each number.

Summary

We started the tutorial by getting insights into the term practical numbers.We further understood the steps to be followed to acquire these numbers. We moved on and saw some examples to understand the concept even better and also did some analysis for the same. Finally, we took a practical approach into account. We wrote a code to observe the practical nature of the numbers ranging between 1 and 20. This is all about practical numbers.


Related Topics

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Java Pop

The array, linked list, stack, queue, and other data structures are supported by Java programming. The insertion, deletion, and element searching operations are available for every data structure. And Java...

4 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Java String toUpperCase() methods

Java String toUpperCase() method is used to convert all the characters of the String into upper case. Syntax public String toUpperCase() public String toUpperCase(Locale locale) Returns It returns upper case String. Java String toUpperCase() Example 1: public...

1 minute read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Collection Interfaces in Java with Examples

In this tutorial, we will discuss collection interfaces in Java with Examples. Introduction The Collection Interface is an individual from the Java Collections Framework. It is a root point of interaction of...

4 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Perfect Number Program in Java

Perfect Number Program in Java A perfect number is a number whose sum of all the factors, excluding the number itself, is equal to the number. For example, 28 is a...

4 minutes read.

Inheritance Program in Java

Inheritance Program in Java Inheritance is one of the important pillars of Object-Oriented Programming that facilitates parent-child relationships in programming. Using inheritance, we can create a new class with the help...

7 minutes read.

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not. Syntax: public boolean isEmpty() Returns It returns true, if length of String is 0 otherwise false. Java String isEmpty() method example 1    ...

1 minute read.