Champernowne Constant

09Aug13

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.

Champernowne Constant

The Champernowne constant is defined by concatenating consecutive integers: “0.123456789101112…”. The GCF of a set of numbers is the greatest number that divides equally into all numbers in that set (2 is the GCF of 4 and 6 for example). To reduce a fraction to simplest form, you divide the numerator and denominator (top and bottom parts) by the GCF of both the numerator and denominator (reducing 2/6 to simplest form produces 1/3 for example). When the Champernowne constant is rounded to 4 decimal places, it produces the fraction – in simplest form – of 247/2000. Find the fraction in simplest form that is produced when rounding the Champernowne constant to 100 decimal places.

Solution and answer (Champernowne_Constant.java):

/*
Solution and answer for problem "Champernowne Constant" (9th August 2013) of http://ContestCoding.WordPress.com/

Fri Aug 09 09:00:00 GMT+01:00 2013

617283945505560657075808590960106111621263136414651566166717681869197020712172227323742475257626773/5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Fri Aug 09 09:00:00 GMT+01: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.
*/

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

public class Champernowne_Constant
{
  private static BigInteger numerator( int p )
  {
    String s = "";
    int i = 1;
    do
    {
      s += i ++;
    }
    while( s.length() < p + 1 );
    char c = s.charAt( p );
    s = s.substring( 0, p );
    BigInteger bI = new BigInteger( s );
    if( c >= '5' )
      bI = bI.add( new BigInteger( "1" ));
    return bI;
  }

  private static BigInteger denominator( int p )
  {
    String s = "1";
    for( ; p > 0; p -- )
      s += "0";
    return new BigInteger( s );
  }

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

    final int NUMBER_OF_DIGITS = 100;
    BigInteger numerator = numerator( NUMBER_OF_DIGITS );
    BigInteger denominator = denominator( NUMBER_OF_DIGITS );
    BigInteger gCF = numerator.gcd( denominator );
    System.out.println( "\n" + numerator.divide( gCF ) + "/" + denominator.divide( gCF ) + "\n" );

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


No Responses Yet to “Champernowne Constant”

  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: