×

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

Java Integer rotateRight() method

The rotateRight() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value right by the specified number of bits. Syntax public...

1 minute read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

How to Convert Decimal to Binary in Java

How to Convert Decimal to Binary in Java There are two methods to convert Decimal to Binary. Using toBinaryString() method Using user-defined logic Using Integer.toBinaryString() The toBinaryString() is a static method of Integer...

2 minutes read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Java Generate Random String

Java Generate Random String In this tutorial, we will learn about to generate random string in Java. Random generation strings mean any string will be generated, which does not follow any...

7 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Java BufferedWriter

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

4 minutes read.

Java Case Keyword

Case keyword: In this article we are going to learn the concept of a java case keyword.Generally, java case keyword is used with the switch statements.Case keyword is implemented in conditional...

3 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

Three-way operator in Java

The ternary operator in Java is the only conditional operator that takes three operands. It's a popular one-line substitute for the if-then-else expression among Java programmers. If-else clauses can be...

3 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Enterprise Java Beans

One of the many Java APIs for the common development of corporate software is Enterprise Java Beans (EJB). An EJB, a server-side software component, contains the business logic of an...

4 minutes read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.