×

Trimorphic numbers in Java

Wondered what the Trimorphic number is!! In this article, you can learn about what a Trimorphic number is referred to as and how to find whether a number is trimorphic or not using java code.

Trimorphic Number :

A number is said to be a trimorphic number if it ends or terminates with itself when we raise it to a power of 3.

For simple understanding , if you take a number and multiply itself three times, if the resultant number ends with the digits of that number, then it is said to be a trimorphic number .

Consider the following example:

Let n = 75 ,

Raise 75 to the power of 3.

That is , 75 * 75 * 75 which is equal to 4,21,875 . The final resultant number ends with 75. Hence, it is a trimorphic number.

More simply, you can say that if the cube of a number ends with itself, then we can say that a number is a trimorphic number , else it is not a trimorphic number.

Step to find out whether the given number is trimorphic or not :

Step 1:  Take a number and raise it to a power of 3 that is multiply itself three times.

Step 2 : Find the number of digits in the given input and store the value to the variable  ( “ x ” ) for further use.

Step 3 : Find out the last “x” digits of the cube value , and compare them with the given input.

Step 4: If the given number and the last " x " digits of the computed value match each other, then you can fix that the given number is a trimorphic number .

Trimorph.java

public class TrimorpNum  
{  
  


public int findDigiCount(int X)  
{  
    int cDigi = 0;  
      
    while(X != 0)  
    {  
        X = X / 10;  
        cDigi = cDigi + 1;  
    }  
      
      
    return cDigi;  
}  
  


public int finPow(int B, int E)  
{  
    int answer = 1;  
      
    while(true)  
    {  
        if((E % 2) == 1)  
        {  
            answer = answer * B;  
            E = E - 1;  
        }  
          
        if(E == 0)  
        {  
            return answer;  
        }  
          
        B = B * B;  
        E = E / 2;  
    }  
      
}  
  


public boolean chekTrimorph(int T)  
{  
    // step 1  
    int Y = T * T * T;  
      
    // second step process
    int N = findDigiCount(T);  
      
    // third step process
    int P = finPow(10, N);  
      
    // fourth step process
    int R = Y - T;  
      
    // fifth step process
    return (R % P) == 0;  
      
}  
  
public static void main (String args [ ])  
{  
// object creation for the class TrimorpNum  
TrimorpNum object = new TrimorpNum();  
  
for (int i = 1; i <= 10; i++)  
{  
boolean isTrimorph = object.chekTrimorph(i);  
if(isTrimorph)  
{  
System.out.println(i + " is perfectly a Trimorphic Number.");   
}  
else  
{  
System.out.println(i + " is not a Trimorphic Number. Try another .");   
}  
}  
}  
}  

Output :

Trimorphic numbers in Java

TrimorphNum.java

public class TrimorpNum  
{  
// a method that checks whether the number T is trimorphic or not  
public boolean chekTrimorp (int Tri)  
{      
    int Z = Tri * Tri * Tri ;  
    String Tri1 = String.valueOf (Tri) ;  
    String Z1 = String.valueOf (Z) ;  
    int si1 = Tri1.length ();  
    int si2 = Z1.length ();  
    for ( int i = si1 - 1, j = si2 - 1; i >= 0 && j >= 0;)  
    {  
        char a = Tri1.charAt(i);  
        char b = Z1.charAt(j);         
        if (a != b)  
        {  
            return false ;  
        }          
        i = i - 1 ;  
        j = j - 1 ;  
    }  
    return true;      
}  
// main method  
public static void main (String args [ ])  
{  
// creating an object of the class TrimorpNum  
TrimorpNum object = new TrimorpNum ();  
for ( int i = 1; i <= 10; i++)  
{  
boolean isTrimorph = object.chekTrimorp (i);  
if (isTrimorph)  
{  
System.out.println ( i + " is perfectly a trimorphic number .");   
}  
else  
{  
System.out.println ( i + " is not a trimorphic number . Try another .");   
}  
}  
}  
}  

Output:

Trimorphic numbers in Java

Related Topics

How to Create a Package in Java?

For the most part, how to create a package in Java generally, a package is defined as a collection of relevant or irrelevant items together in a very major way....

7 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

Java URL Class with Example

Java URL Uniform Resource Locator To find any resource on the internet, you need to have an address of it. The URL and IP addresses are the pointers used for this purpose....

12 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Sum of digits in string in java

To find the sum of all digits in a string, you need to traverse through the string one by one character; if the character is an integer, you need to...

2 minutes read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Model Class in Java

In this section, we will be acknowledged about the model class in Java, its purpose and its uses. Also, we will learn how is this created and leveraged in java. Model...

4 minutes read.

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.

Diffie Hellman Algorithm in Java

In this section, you will be acknowledged about Diffie Hellman algorithm clearly step wise along with an example and also an example program. Diffie Hellman Algorithm One of the most significant algorithms...

3 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Advanced Java Viva Questions

One of the more difficult languages available now is Java. Currently, 10 thousand developers worldwide use the programming language, which is rising daily. So, if you're a Java developer, an aspiring...

9 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.