Math, asked by labunichowdhury1434, 5 hours ago

Determine all natural numbers divisible by 8 whose sum of digits is less than 10 and the product of digits is equal to 12.

Answers

Answered by Brain9190
0

Answer:

Given a non-negative number n. The problem is to find the smallest number k such that the product of digits of k is equal to n. If no such number k can be formed then print “-1”.

Examples:

Input : 100

Output : 455

4*5*5 = 100 and 455 is the

smallest possible number.

Input : 26

Output : -1

Source: Asked in Amazon Interview

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: For each i = 9 to 2, repeatedly divide n by i until it cannot be further divided or the list of numbers from 9 to 2 gets finished. Also, in the process of division push each digit i onto the stack which divides n completely. After the above process gets completed check whether n == 1 or not. If not, then print “-1”, else form the number k using the digits from the stack containing the digits in the same sequence as popped from the stack.

// C++ implementation to find smallest number k such that

// the product of digits of k is equal to n

#include <bits/stdc++.h>

using namespace std;

// function to find smallest number k such that

// the product of digits of k is equal to n

long long int smallestNumber(int n)

{

// if 'n' is a single digit number, then

// it is the required number

if (n >= 0 && n <= 9)

return n;

// stack the store the digits

stack<int> digits;

// repeatedly divide 'n' by the numbers

// from 9 to 2 until all the numbers are

// used or 'n' > 1

for (int i=9; i>=2 && n > 1; i--)

{

while (n % i == 0)

{

// save the digit 'i' that divides 'n'

// onto the stack

digits.push(i);

n = n / i;

}

}

// if true, then no number 'k' can be formed

if (n != 1)

return -1;

// pop digits from the stack 'digits'

// and add them to 'k'

long long int k = 0;

while (!digits.empty())

{

k = k*10 + digits.top();

digits.pop();

}

// required smallest number

return k;

}

// Driver program to test above

int main()

{

int n = 100;

cout << smallestNumber(n);

return 0;

}

Output:

455

Time Complexity: O(num), where num is the size of the stack.

Space Complexity: O(num), where num is the size of the stack.

We can store the required number k in string for large numbers.

This article is contributed by Ayush Jauhari.

Answered by pruthaasl
0

Answer:

The natural numbers divisible by 8, whose sum of digits is less than 10, and the product of digits equal to 12 are 216, 232, 1232, 2312, 11232, 12312, 21312, 23112, and 32112.

Step-by-step explanation:

It is given that the product of the digits should be equal to 12. So, the digits have to be prime factors of 12.

The prime factors of 12 are 2×2×3.

Keeping in mind that the sum of the digits should be less than 10, we can arrange the prime factors in the following ways:

  1. 1×2×6
  2. 2×2×3
  3. 1×2×2×3
  4. 1×1×2×2×3

The next step is to find all the possible arrangements of the digits 126, 223, 1223, and 11223 that are divisible by 8.

For the number to be divisible by 8, the last 3 digits should be divisible by 8 and the last digit must be an even number.

  • The even permutations of 126 are 126, 162, 216, and 612. Among these, the only number divisible by 8 is 216.
  • The even permutations of 223 are 232, 322. Among these, only 232 is divisible by 8.
  • The even permutations of 1223 are 1232, 1322, 2132, 2312, 3122, and 3212. Among these, 1223 and 2312 are divisible by 8.
  • The even permutations of 11223 are 11232, 11322, 12132, 12312, 13122, 13212, 21132, 21312, 23112, 31122, 31212, 32112. Among these, 11232, 12312, 21312, 23112, and 32112 are divisible by 8.

Therefore, the natural numbers satisfying the given conditions are 216, 232, 1223, 2312, 11232, 12312, 21312, 23112, and 32112.

#SPJ2

Similar questions