Any Idea of algorithm

126 Views Asked by At

I'm a Computer Science guy, I'm given an assessment exercise, but I realize it does involve math, an algorithm.

So the condition : John is a young boy who likes numbers. He thinks the digits in the numbers aren't ordered as they might. For a number to have ordered digits, he writes the number (for example 42) and then this John somehow picks a number (for example 29) from whose digits will be added (2 + 9) and will generate another number - 11, and so on (1+1) till you generate the next number - 2. Finally 42 = 29 + 11 + 2.

I can't understand how John picks the first number to generate the next exact numbers and to be the sum. There must be some algorithm.

In Short: Given a number like 42, I have to find whether there exists another number (in this case 29) where taking the "sum sequence of the digits", gives the first number.

Restrictions: 10 <= John's Number <= 100000. I mean I don't want to loop from 10 to 100000 and check every number (as someone said in the comments) - that's not efficient.

Any idea?

1

There are 1 best solutions below

5
On

I don't have enough reputation to post this as a comment, but it actually can help solve it much more quickly.

Edit: The Maximum Difference I describe here is the maximum difference possible between a number (e.g. 42) and a number whose digits added together repeatedly will equal the first number (e.g. 29, 2+9 = 11, 1 + 1 = 2, 29 + 11 + 2 = 42) This is only a helpful trick that could be used to find these numbers by hand without trying every number below it. It is not an algorithm for solving the OPs problem.

Say you start with a number 5376. 5376 has 4 digits, so the maximum difference between 5376 and a possible picked number is 5 (4+1) * 9 = 45

Meaning that if a number (or numbers) exists, it will be between 5375 (5376-1) and 5331 (5376-45) (inclusive) In addition, the difference seems to only fall on certain numbers within these ranges. Specifically, the difference never falls on 10,12,14,16,18,20,29,31,38,40,47,49,53 (at least within the 100000)

For our example, this means that it cannot be 5366(5376-10), 5364(5376-12), 5362(5376-14)... etc.

For our example, there is only one selected number for 5376. It's 5353(5376-23).

While this doesn't solve it outright, this does reduce it to a number of possibilities for any given number to a set that can be tested by hand or possibly even in your head if you're good enough.

Source: Experimentation

Edit: I should probably add this to my answer to clarify.

The maximum difference for 2 digits is 21 (e.g. 78->99)

The maximum difference for 3 digits is 34 (e.g. 899->933)

The maximum difference for 4 digits is 43 (e.g. 9899->9942)

The maximum difference for 5 digits is 54 (e.g. 99939->99993)

The maximum difference for 6 digits is 66 (e.g. 999895->999961)

The maximum difference for 7 digits is 78 (e.g. 9999896->9999974)

The maximum difference for 8 digits is 90 (e.g. 99999897->99999987)

We can see from numbers outside the given set that the rule no longer holds, but for the given set, it's still something John could memorize as a numerical trick that would allow him to solve them in his head. This only works because (5+1)*9 = 54 >= 54, (4+1)*9 = 45 >= 43, (3+1)*9 = 36 >= 34, and (2+1)*9 = 27 >= 21. You could just as easily use 5*11 + 1 = 56 >= 54, 4*11 + 1 = 45 >= 43, 3*11 + 1 = 34 > = 34, 2*11 + 1 = 23 >=21