Calculate seed of a number in Python
In this tutorial, we will learn how to find the seed of a number in Python.
Definition of seed
A number x is said to be the seed of a number n if :
x * product of digits of x is equal to n.
Given a number n, find all the seeds of the number.
- Input- 4977
Output- 79 , 711
79 * 7 * 9 =4977
711 * 7 * 1 * 1
- Input – 111
111 * 1 = 111
- Input- 738
123 * 1 * 2 * 3 = 738
- Input- 470
It does not have any seed.
Python program to calculate seed of a number
Check for all numbers from 1 to n.
We need to check only for those numbers which are divisible by n (i.e n%i==0) otherwise, they can not be the seed of the given number.
For the numbers, which are divisible, we find the product of its’s number.
If ProductOf Digits(i)*i ==n, then i is the seed of the given number.
To find the product of digits, initialize prod with 1, and multiply (i%10) on every iteration until i is greater than 0, on every iteration divide i by 10.
def ProdOfDigits(i): if i < 10: return i prod=1 while(i!=0): prod=prod*(i%10) i=i//10 return prod n=4977 for i in range(1 , n+1): if n%i==0: if ProdOfDigits(i)*i==n: print(i)
You may also read:
- Python program for converting a list of characters into a string
- C++ code that will find parity of a number