Click here to Skip to main content
13,247,336 members (89,604 online)
Rate this:
Please Sign up or sign in to vote.
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 6:19am
Updated 17-Jul-17 6:24am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

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
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!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171114.1 | Last Updated 17 Jul 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100