Find Seed of a Number
A number X is said to be 'seed' of number Y if multiplying X by its digit equates to Y.
For example, 123 is a seed of 738 coz 123*1*2*3 = 738. Now given a number find all its 'seed'.
Thanks for this. I got an idea to get started with this problem because of this solution. Some improvement suggestions -
1. This code doesn't work for 24 = 12 * 1 * 2. This is because your seed stops looping at value 11. Just change it to seed <= (num * 0.5)
2. Doesn't work for 111 = 111 * 1 * 1 * 1. For this case to be handled, we'll have to expand the loop for seed to loop all the way until num, which is while (seed <= num). Seems inefficient. The other alternative is to somehow check if all digits are ones.
3. We can initially check if num < 10. If yes, then that itself is the seed. Hence we don't need to enter the while loop. This also gives us the flexibility to initialize seed to 10 and start with 10, rather than 1.
Read full article from Find Seed of a Number
A number X is said to be 'seed' of number Y if multiplying X by its digit equates to Y.
For example, 123 is a seed of 738 coz 123*1*2*3 = 738. Now given a number find all its 'seed'.
int main()
{
int num = 1;
cout << "Enter the number: ";
cin >> num;
int product = 1;
int seed = 1;
while (seed < (num * 0.5)){
if (num % seed == 0){
int factor = seed;
product = factor;
while(!(factor == 0)){
int mod = factor % 10;
factor = factor / 10;
product *= mod;
}
if (product == num){
cout << "\nThe seed of " << num << " is: " << seed;
return 0;
}
}
seed++;
}
}
Seed is always greater than sqrt(N) Thanks for this. I got an idea to get started with this problem because of this solution. Some improvement suggestions -
1. This code doesn't work for 24 = 12 * 1 * 2. This is because your seed stops looping at value 11. Just change it to seed <= (num * 0.5)
2. Doesn't work for 111 = 111 * 1 * 1 * 1. For this case to be handled, we'll have to expand the loop for seed to loop all the way until num, which is while (seed <= num). Seems inefficient. The other alternative is to somehow check if all digits are ones.
3. We can initially check if num < 10. If yes, then that itself is the seed. Hence we don't need to enter the while loop. This also gives us the flexibility to initialize seed to 10 and start with 10, rather than 1.
private static void seed(int i) {
ArrayList<Integer> factor=new ArrayList<Integer>();
for(int j=2;j<=i/2;j++){
if(i%j==0)
factor.add(j);
}
int temp=1;
for(int number:factor){
int temp2=number;
temp=number;
while(number>;0){
temp*=number%10;
number=number/10;
}
if(temp==i)
System.out.println(temp2);
}
Read full article from Find Seed of a Number