Program to Check Trimorphic Number or Not

Python Program to Check Trimorphic Number or Not

In the previous article, we have discussed Python Program to Check Abundant Number or Not
Trimorphic Number:

Cube the number you’ve been given. If the given number appears in the cube number in the last means, it is referred to as a trimorphic number.

For example:

Let the given number =25

The cube of the number = 25*25*25= 15625

The Given number 25 is present at the end.

Therefore, 25 is a Trimorphic Number.

The examples of the first few Trimorphic Numbers are 1, 4, 5, 6, 9, 24, 25, 49, 51, 75, 76, 99, 125, 249, 251, 375, 376, 499, and so on.

Given the number and the task is to check whether the given number is a Trimorphic Number or not.

Examples:

Example1:

Input:

Given Number = 75

Output:

The given number { 75 } is a Trimorphic number

Example2:

Input:

Given Number = 30

Output:

The given number { 30 } is Not a Trimorphic number

Program to Check Trimorphic Number or Not in Python

Below are the ways to check whether the given number is a Trimorphic Number or not:

Method #1: Using Slicing (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Calculate the cube root of the given number and convert it into the string using the str() function.
  • Store it in another variable.
  • Convert the given number to a string using the str() function and store it in another variable.
  • Calculate the length of the above-obtained string number using the len() function and store it in another variable.
  • Check if the cube_root[-len_string:](negative slicing)  is equal to the string number using the if conditional statement and slicing.
  • If the statement is true, then print “The given number is a Trimorphic Number”.
  • If it is false, then print “The given number is Not a Trimorphic Number”.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 75
# Calculate the cube root of the given number and convert it into the string using the str() function.
# Store it in another variable.
cube_numb = str(gvn_numb**3)
# Convert the given number to a string using the str() function and store it in
# another variable.
str_numb = str(gvn_numb)
# Calculate the length of the above-obtained string number using the len() function
# and store it in another variable.
len_str = len(str_numb)
# Check if the cube_root[-len_string:](negative slicing)  is equal to the string number using the
# if conditional statement and slicing.
if(cube_numb[-len_str:] == str_numb):
  # If the statement is true, then print "The given number is a Trimorphic Number".
    print("The given number {", gvn_numb, "} is a Trimorphic number")
else:
  # If it is false, then print "The given number is Not a Trimorphic Number".
    print("The given number {", gvn_numb, "} is Not a Trimorphic number")

Output:

The given number { 75 } is a Trimorphic number

Method #2: Using Slicing (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Calculate the cube root of the given number and convert it into the string using the str() function.
  • Store it in another variable.
  • Convert the given number to a string using the str() function and store it in another variable.
  • Calculate the length of the above-obtained string number using the len() function and store it in another variable.
  • Check if the cube_root[-len_string:](negative slicing) is equal to the string number using the if conditional statement and slicing.
  • If the statement is true, then print “The given number is a Trimorphic Number”.
  • If it is false, then print “The given number is Not a Trimorphic Number”.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Calculate the cube root of the given number and convert it into the string using the str() function.
# Store it in another variable.
cube_numb = str(gvn_numb**3)
# Convert the given number to a string using the str() function and store it in
# another variable.
str_numb = str(gvn_numb)
# Calculate the length of the above-obtained string number using the len() function
# and store it in another variable.
len_str = len(str_numb)
# Check if the cube_root[-len_string:](negative slicing) to the string number using the
# if conditional statement and slicing.
if(cube_numb[-len_str:] == str_numb):
  # If the statement is true, then print "The given number is a Trimorphic Number".
    print("The given number {", gvn_numb, "} is a Trimorphic number")
else:
  # If it is false, then print "The given number is Not a Trimorphic Number".
    print("The given number {", gvn_numb, "} is Not a Trimorphic number")

Output:

Enter some random number = 30
The given number { 30 } is Not a Trimorphic number

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.