-Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 134468.

+Working from left-to-right if no digit is exceeded by the digit to its left it

+is called an increasing number; for example, 134468.

-Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 66420.

+Similarly if no digit is exceeded by the digit to its right it is called a

+decreasing number; for example, 66420.

-We shall call a positive integer that is neither increasing nor decreasing a "bouncy" number; for example, 155349.

+We shall call a positive integer that is neither increasing nor decreasing a

+"bouncy" number; for example, 155349.

-Clearly there cannot be any bouncy numbers below one-hundred, but just over half of the numbers below one-thousand (525) are bouncy. In fact, the least number for which the proportion of bouncy numbers first reaches 50% is 538.

+As n increases, the proportion of bouncy numbers below n increases such that

+there are only 12951 numbers below one-million that are not bouncy and only

+277032 non-bouncy numbers below 1010.

-Surprisingly, bouncy numbers become more and more common and by the time we reach 21780 the proportion of bouncy numbers is equal to 90%.

-Find the least number for which the proportion of bouncy numbers is exactly 99%.

+How many numbers below a googol (10100) are not bouncy?

push @d_counts, (0+$digit_sum);

+ # print "For $digit_idx : ", join(",", reverse @d_counts ), "\n";

push @counts, [reverse @d_counts];

+ # We don't need to count the zero.

+ my $NUM_DIGITS = shift;

+ my $max_digit = $NUM_DIGITS - 1;

# Count the decreasing numbers

# For N = 1 the decreasing numbers are 0-9.

# For N = 2 the decreasing numbers are 0-9,10,11,20,21,22,30,31,32,33,...

# For N = 3 the decreasing numbers are

+ my @d_counts = ((1) x @digits);

+ push @counts, \@d_counts;

- push @counts, [(1) x @digits];

- foreach my $digit_idx (1 .. $max_digit)

+ foreach my $d_c (@d_counts[1 .. 9])

- foreach my $d (@digits)

- $digit_sum += $counts[-1][$d];

- push @d_counts, (0+$digit_sum);

- push @counts, \@d_counts;

- foreach my $d_c (@d_counts[1 .. 9])

+ foreach my $digit_idx (1 .. $max_digit)

+ foreach my $d (@digits)

+ $digit_sum += $counts[-1][$d];

+ push @d_counts, (0+$digit_sum);

+ push @counts, (\@d_counts);

+ # print "For $digit_idx : ", join(",", reverse @d_counts ), "\n";

+ foreach my $d_c (@d_counts[1 .. 9])

+ my $NUM_DIGITS = shift;

+ my $max_digit = $NUM_DIGITS - 1;

+ my $total_sum = count_increasing($NUM_DIGITS) + count_decreasing($NUM_DIGITS);

# Remove one count of the numbers that are both increasing and

# decreasing i.e: numbers with all digits the same.

- foreach my $num_digits (~~1~~ .. $max_digit)

+ foreach my $num_digits (0 .. $max_digit)

- $total_sum -=~~ $num_digits *~~ 9;

- print "Total($NUM_DIGITS) = ", calc_num_non_bouncy($NUM_DIGITS), "\n";

+ print "Both($NUM_DIGITS) = ", calc_num_non_bouncy($NUM_DIGITS), "\n";

+ print "Inc($NUM_DIGITS) = ", count_increasing($NUM_DIGITS), "\n";

+ print "Dec($NUM_DIGITS) = ", count_decreasing($NUM_DIGITS), "\n";

+ my ($inc_count, $dec_count, $both_count);

+ for my $n (1 .. (10 ** $NUM_DIGITS-1))

+ my $s = join "",sort { $a <=> $b } split//,$n;

+ $both_count += $both_offset;

+ print "Real_Count_Both($NUM_DIGITS) = $both_count\n";

+ print "Real_Count_Inc($NUM_DIGITS) = $inc_count\n";

+ print "Real_Count_Dec($NUM_DIGITS) = $dec_count\n";