13,504,005 members
See more:
There are magic numbers comprising of only 1 and 0, such as 0, 1, 10, 11, 100, 101, 110, 111..... and so on. These are integer values and not binaries.

The question is to achieve a sum with minimum usage of magic numbers.

eg: 23 : 11 + 11+ 1 ( 3 magic numbers)
120: 110+ 10 (2 magic numbers)
What can be the logic to find the minimum magic numbers required to calculate the sum?

What I have tried:

the trick here is to get the max digit in the value. e.g: for 23,the minimum magic number will be 3 and for 120 it will be 2, for 651 it will be 6.

I am trying to implement/code the logic to find the combination of the magic numbers required to get the sum, but I am not sure how to proceed with the code.
The logic is stated below:
Step 1: get the length of the value.
Step 2: Divide the value by 10 and get the modulus. If the remainder is 0 then the unit digit of the magic number will be 0 or if its more than 0 than the unit digit will be 1. but how to fill down the ten's or hundred's position???
i am facing difficulty to code it down. any help appreciated
Posted 17-Jul-17 5:19am
Updated 17-Jul-17 5:24am

## Solution 1

If you take a number - for example 1234 - and take the modulous ten on it:
`1234 % 10 = 4`

If you then integer divide the original number by ten, and take the modulus of that:
```1234 / 10 = 123
123 % 10 = 3```
Which is as you can see the "tens" digit. Repeating the operations gives you the hundreds:
```123 / 10 = 12
12 % 10 = 2```
And so on.
Shibu Mahapatra 17-Jul-17 11:30am

This is fine @OriginalGriff, but still I am unable to code it down... sorry for being so dumb
OriginalGriff 17-Jul-17 11:37am

What part of it can't you code?
You know how to write a loop, you know how to get a modulus, you know how to divide integers. I assume you can write an if statement, so which bit of this is difficult for you?
shankar077 17-Jul-17 11:49am

how to generate the magic number? suppose 120%10 , i know that the unit place is 0, but how do i fill the ten's and hundred's position?
OriginalGriff 17-Jul-17 12:00pm

Let's start at the beginning, shall we...
You need to find the largest digit, yes?
So what you need to do that? (This is not a trick question)
Answer: look at each digit individually, and find out the largest value.
So ... you need a loop. Can you write the outside of the loop yourself? (Hint: you want a *while* loop)
shankar077 17-Jul-17 12:25pm

That is easy.
int largest=0;
int no=546;
while (no > 0) {
if (no % 10 > largest) {
largest = no % 10;
}
no = no / 10;
}
OriginalGriff 17-Jul-17 12:39pm

Yay!
So ... you have the largest digit when the loop is finished.
And that means you know the minimum magic number...
shankar077 17-Jul-17 13:44pm

Thanks @OriginalGriff, but this is just one part of the question.. The second half I need is the list of magic numbers :). e.g for 120, the set of magic numbers is 110 and 10.
OriginalGriff 17-Jul-17 14:00pm

So what have you tried?
Remember, this is your homework, not ours - you you will have to do it. We'll give kicks in directions occasionally, but the work is yours, not ours.
How would you do it manually?
shankar077 17-Jul-17 23:58pm

well, i will check the units digit of the sum, if its 0 then the max magic number that is less than the sum will end with 0 else end with 1.
eg: 120: the unit is 0 hence the max magic number less than 120 is 110

second step is the subtract 110 from 120. then repeat the first step for the residual value.

I am actually stuck at the step, that how to fill the other position in the value except the units digit
OriginalGriff 18-Jul-17 3:59am

You are stuck at writing x - y in a loop?
Come on! You know better than that!

Top Experts
Last 24hrsThis month
 OriginalGriff 148 Wendelius 78 John Simmons / outlaw programmer 75 Richard MacCutchan 70 Dave Kreskowiak 55
 OriginalGriff 3,963 Jochen Arndt 2,328 ppolymorphe 2,013 Maciej Los 1,654 Wendelius 1,558