Project Euler Problem #112!

December 3rd, 2009
by Serinox

Problem #112 says:

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.

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.

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%.

This is pretty simple. we just need a function that can tell us if a number qualifies as “bouncy” which means it can’t be increasing or decreasing. So we create three functions and then start counting. Solution below is in c#/mono and runs in well under a minute

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ProjectEuler
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();

            double Count = 1;
            double NumBouncy = 0;

            while (NumBouncy / Count != .99)
            {
                Count++;
                if (Count % 50000 == 0)
                    Console.WriteLine(NumBouncy/Count);
                if (IsBouncy(Count))
                {
                    NumBouncy++;
                }

            }

            Console.WriteLine(Count);
            Console.WriteLine(sw.Elapsed);
            Console.ReadLine();
        }
        static bool IsBouncy(double l)
        {
            if (IsIncreasing(l))
                return false;
            if (IsDecreasing(l))
                return false;
            return true;
        }
        static bool IsIncreasing(double l)
        {
            char[] digits = l.ToString().ToCharArray();

            for (int i = 0; i < digits.Length-1; i++)
            {
                if (Convert.ToInt32(digits[i].ToString()) > Convert.ToInt32(digits[i+1].ToString()))
                {
                    return false;
                }
            }

            return true;
        }
        static bool IsDecreasing(double l)
        {
            char[] digits = l.ToString().ToCharArray();
            Array.Reverse(digits);

            for (int i = 0; i < digits.Length - 1; i++)
            {
                if (Convert.ToInt32(digits[i].ToString()) > Convert.ToInt32(digits[i + 1].ToString()))
                {
                    return false;
                }
            }
            return true;
        }
    }

}

Posted in Project Euler | Comments (0)

No comments yet

Leave a Reply

You must be logged in to post a comment.