Increasing Digits

06Sep13

In the twelve months from March 2013 to March 2014, I programmed solutions to the problems posted on the Contest Coding blog run by Lewis Cornwall, solving 33 problems (out of 47) and achieving a position of 4th on the leaderboard (out of 23).

As that blog has now been discontinued, I’m posting here the solutions I programmed to those problems.

Increasing Digits

There are 36 2-digit integers such that every digit in that integer increases from left to right (12, 13, 14, … 23, 24 etc). Find the total number of 4-digit integers such that every digit in that integer increases from left to right.

Solution and answer (Increasing Digits.pas):

{$R+}
program Increasing_Digits( output );
{
Solution and answer for problem "Increasing Digits" (6th September 2013) of http://ContestCoding.WordPress.com/

126

Solution programmed in Pascal using Metrowerks CodeWarrior IDE 2.1 (Discover Programming Edition); solution took ~1s to run on a 80MHz PowerPC 601.
}
var
  total : 0..8999;
  i : 1000..9999;
  last_digit : 0..9;
  k : 0..999;
  digit : 0..9;
  b : boolean;
begin
  total := 0;
  for i := 1000 to 9999 do begin
    last_digit := i mod 10;
    k := i div 10;
    repeat
      digit := k mod 10;
      k := k div 10;
      b := digit < last_digit;
      last_digit := digit
    until ( k = 0 ) or not b;
    if b then
      total := total + 1
  end;
  writeln( total )
end.
Advertisements


No Responses Yet to “Increasing Digits”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: