-2

Summary

An increasing number in this case would be one whose digits are increasing as they move from left to right, such as 14578 and 3489 and 3347778.

A decreasing number would be the same, except reversed, such as 84320 and 931.

Given a number x between 1 and 10, how would you count the total count of numbers that increase but not decrease, or decrease but not increase, without using a brute force/iteration type of approach?


Sample chart for first few values of x

+---+-------+
| X | Total |
+---+-------+
| 0 |     1 |
| 1 |    10 |
| 2 |   100 |
| 3 |   475 |
| 4 |  1675 |
| 5 |  4954 |
+---+-------+
Spooky
  • 1,752
  • 21
  • 37
  • To clarify, is `X` the number of digits? Does `Total` include both increasing and decreasing? I'm also not sure that this belongs here - since you specifically say not to use brute force/iteration, this doesn't seem to have anything to do with programming. Maybe mathematics / puzzling SE would be a better fit? – user812786 Mar 16 '16 at 15:38
  • X has to do with the range. Total is every increasing and decreasing number that is less than 10 to the X power. – Spooky Mar 16 '16 at 15:41
  • So X is the number of digits? for X=6, 10^6 = 1000000, which if your range is less than the max, would include 999999 but not 1000000, which has 6 digits. I think you should look at probability theory to come up with a formula - for example, given a digit N (e.g. 7), there are N possible digits (e.g. 0 to 6) that can follow N and still be descending... etc. It wouldn't be small, but it would be a discrete computation rather than brute-force/iterative. – Matt Jordan Mar 16 '16 at 15:54
  • It doesn't include the top of the range, but the range goes all the way down to 0. So saying X is the number of digits isn't accurate. Your idea with the discrete computation is worth a shot though. – Spooky Mar 16 '16 at 15:58
  • *"Total is every increasing and decreasing number that is less than 10 to the X power."* - if that were true, when x == 1 you'd count increasing and decreasing numbers less than 10^1, i.e. 10. There are no increasing or decreasing numbers less than 10, so your interpretation of your own question doesn't match the totals you've provided. – Tony Delroy Mar 16 '16 at 15:59
  • @TonyD sorry about that, fixed the table. – Spooky Mar 16 '16 at 16:02
  • Doesn't seem fixed... total should be 0 for x of 0 or 1, then for x == 2 you want increasing and decreasing numbers in the range 0..99: clearly two digits are needed to be increasing or decreasing, so for increasing sequences we have e.g. 12, 13... 19, (8 such values) 23, 24... 29, (7 such values) ..., 89 (1 value) - i.e. 8+7+6+5+4+3+2+1 = 36. For descending: 98, 97... 90 (9 such values), 87, 86... 80 (8 such values), ..., 10 (1 such value), i.e. 9 values more than ascending = 45. 36 + 45 = 81. 81's not in your table. You're not making the effort to present a coherent question. – Tony Delroy Mar 16 '16 at 16:26
  • @TonyD First of all, 10^0 is 1. So you've got 0 right there, which counts. Second of all, the chart is correct. ALL numbers in the range of 0-100 have digits that don't switch between increasing and decreasing. If I have to spell it out for you, I will. 00, 01, 02, ... (10 values), 10, 11, 12, 13... (another 10), and so on. You're the one not making an effort to read the question well enough, so keep snide comments to yourself please. – Spooky Mar 16 '16 at 16:50
  • 1
    Please give a link to exact problem definition. Your explanation is not clear – MBo Mar 16 '16 at 18:56

1 Answers1

7

If you want to calculate the number of increasing numbers, you can build a 2D table, where the rows correspond to the involved numbers and the columns to the number of digits. And you insert the number of increasing numbers with i digits, using digits up to j in the cells. The first column is easy:

      1 digit  2 digits  3 digits ...
>= 0     10
>= 1     9
>= 2     8
>= 3     7
>= 4     6
>= 5     5
>= 6     4
>= 7     3
>= 8     2
>= 9     1

If you want to calculate the next column, you can refer to the previous one. Suppose you want to calculate cell(2, 9) (two-digit numbers with digits greater than 9). Then you can put a 9 on front and use any 1-digit number with digits greater than 9. So, just 1. The same goes for cell(2, 8). You can put an 8 at front plus any 1-digit number with digits greater than 8 (cell(1, 8)) plus the same numbers you used for the below cell. In general:

cell(i, j) = cell(i - 1, j) + cell(i, j + 1)

The first few columns are:

      1 digit  2 digits  3 digits ...
>= 0     10    10+45=55   55+165=220
>= 1     9     9+36=45    45+120=165
>= 2     8     8+28=36    36+84=120
>= 3     7     7+21=28    28+56=84
>= 4     6     6+15=21    21+35=56
>= 5     5     5+10=15    15+20=35
>= 6     4     4+ 6=10    10+10=20
>= 7     3     3+ 2=6      6+ 4=10
>= 8     2     2+ 1=3      3+ 1=4
>= 9     1          1           1

The total number is always the entry cell(digits, 0). So there are 220 increasing numbers with three or less digits.

A similar table can be calculated for decreasing numbers. However, in order to calculate the number of decreasing numbers, you have to sum all columns. E.g. for three-digit decreasing numbers: 10+55+220=285.

In order to calculate the increasing or decreasing numbers, just sum the two values and subtract numbers that are both increasing and decreasing. For every digit, it there will be 10. So for 3-digit numbers, you have to calculate 220+285-30=475.

So, calculate the table iteratively. You just need to keep the current column and calculate the total number accordingly.

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70