Python Program to Calculate Seed of a Number

We’ll learn how to find the seed of a number in Python in this article.

Seed of a Number:

If a number x is the seed of a number n, then:

x * the product of x’s digits equals n.

Given a number, the task is to print all the seeds of the given number.

Examples:

Example1:

Input:

Given Number =4977

Output:

79
711

Example2:

Input:

Given Number = 138

Output:

23

Program to Calculate Seed of a Number in Python

Below are the ways to print the seed of a number in Python

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Loop from 1 to given number using the For loop.
  • Check If the iterator value divides the given number leaving remainder 0 or not using the If conditional statement.
  • Convert the given iterator value into list of digits using list(),map(),int() and split() functions.
  • Take a variable proddig and initialize its value to 1.
  • Loop in this digits list using another For loop(Nested For loop).
  • Inside the inner for loop multiply the proddig with the number.
  • After the end of the inner For loop Check if proddig*iterator value is equal to the the given number using the If conditional statement.
  • If it is true then print the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvnnumb = 4977
# Loop from 1 to given number using the For loop.
for it in range(1, gvnnumb+1):
        # Check If the iterator value divides the given number
    # leaving remainder 0 or not using the If conditional statement.
    if(gvnnumb % it == 0):
                # Convert the given iterator value into
        # list of digits using list(),map(),int() and split() functions.
        numbdigi = list(map(int, str(it)))
        # Take a variable proddig and initialize its value to 1.
        proddig = 1
        # Loop in this digits list using another For loop(Nested For loop).
        for digit in numbdigi:
                # Inside the inner for loop multiply the proddig with the number.
            proddig = proddig*digit

        # After the end of the inner For loop Check if proddig*iterator

        # value is equal to the the given number using the If conditional statement.
        if(proddig*it == gvnnumb):
            # If it is true then print the iterator value.
            print(it)

Output:

79
711

Explanation:

79 * 7 * 9 = 4977
711*1*1*7=4977

Method #2: Using For Loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Loop from 1 to given number using the For loop.
  • Check If the iterator value divides the given number leaving remainder 0 or not using the If conditional statement.
  • Convert the given iterator value into list of digits using list(),map(),int() and split() functions.
  • Take a variable proddig and initialize its value to 1.
  • Loop in this digits list using another For loop(Nested For loop).
  • Inside the inner for loop multiply the proddig with the number.
  • After the end of the inner For loop Check if proddig*iterator value is equal to the the given number using the If conditional statement.
  • If it is true then print the iterator value.
  • 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.
gvnnumb = int(input('Enter some random number = '))
# Loop from 1 to given number using the For loop.
for it in range(1, gvnnumb+1):
        # Check If the iterator value divides the given number
    # leaving remainder 0 or not using the If conditional statement.
    if(gvnnumb % it == 0):
                # Convert the given iterator value into
        # list of digits using list(),map(),int() and split() functions.
        numbdigi = list(map(int, str(it)))
        # Take a variable proddig and initialize its value to 1.
        proddig = 1
        # Loop in this digits list using another For loop(Nested For loop).
        for digit in numbdigi:
                # Inside the inner for loop multiply the proddig with the number.
            proddig = proddig*digit

        # After the end of the inner For loop Check if proddig*iterator

        # value is equal to the the given number using the If conditional statement.
        if(proddig*it == gvnnumb):
            # If it is true then print the iterator value.
            print(it)

Output:

Enter some random number = 138
23

Explanation:

23*2*3=138

Related Programs: