Base 36

15Dec13

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.

Base 36

To convert between bases, follow the instructions at this website. When using base 36, all the digits 0-9 and all the letters A-Z are used. Find the decimal (base 10) equivalent of the base 36 number “CONTESTCODING”.

Solution and answer (Base_36.java):

import java.util.Date;
import java.math.BigInteger;

/*
Solution and answer for problem "Base 36" (15th December 2013) of http://ContestCoding.WordPress.com/

Sun Dec 15 09:00:00 GMT+00:00 2013

60106575588567168940

Sun Dec 15 09:00:00 GMT+00:00 2013

Solution programmed in Java using Metrowerks CodeWarrior IDE 2.1 (Discover Programming Edition); solution took ~1s to run on a 80MHz PowerPC 601.
*/

public class Base_36
{
  public static void main( String p[] )
  {
    System.out.println( new Date());

    System.out.println( "\n" + base36StringToBase10BigInteger( "CONTESTCODING" ) + "\n" );

    System.out.println( new Date());
  }

  private static BigInteger base36StringToBase10BigInteger( String p )
  {
    p = p.toLowerCase();
    BigInteger bI = new BigInteger( "0" );
    BigInteger multiplier = new BigInteger( "1" );
    final BigInteger THIRTY_SIX = new BigInteger( "36" );
    for( int i = p.length() - 1; i >= 0; i -- )
    {
      int k = p.charAt( i );
      if( k >= '0' && k <= '9' )
        k -= '0';
      else
      {
        k -= 'a';
        k += 10;
      }
      bI = bI.add( new BigInteger( "" + k ).multiply( multiplier ));
      multiplier = multiplier.multiply( THIRTY_SIX );
    }
    return bI;
  }
}
Advertisements


No Responses Yet to “Base 36”

  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: