Assignment title: Information
Computer program
CITS5502
2016 semester 2
The problem below appears in the ACM International Collegiate Programming Contest in 2016. It is a
reasonably easy problem to solve. Your task for the CITS5502 assignment is to write a computer
program of your chosen (favourite) programming language to print the largest base number B for the
problem described below.
The Forever Young problem:
John's birthday is coming up. Alas, he is getting old and would like to feel young again. Fortunately, he
has come up with an excellent way of feeling younger: if he writes his age as a number in an
appropriately chosen base B, then it appears to be smaller. For instance, suppose that his age in base 10
is 32. Written in base 16, it is only 20!
However, John would not like to choose an arbitrary base when doing this. If his age written in base B
contains digits other than 0 to 9, then it will be obvious that he is cheating, which defeats the purpose. In
addition, if his age written in base B is too small then it would again be obvious that he is cheating.
Given his age Y and a lowerbound L that he wants his age to appear, find the largest base B such that,
when Y is written in base B, it contains only decimal digits and is at least L when interpreted as a number
in base 10.
The input to the program consists of a single line containing two base 10 integers separated by a space
character. The first integer is Y and the second integer is L. You may assume that 10≤Y≤1,000,000
(yes, John is very old) and 10≤L≤Y .
The program should output the largest base B as described above.
Some examples are given below:
Sample input Sample output
Example 1 32 20 16
Example 2 2016 125 42
Example 3 138573 1278 35
In Example 1, the value Y=32 (in decimal) is represented in base 16 as 20 which, when interpreted as a
number in base 10, is exactly equal to L. In Example 2, the value Y=2016 is represented in base 42 as
160 which is larger than 125 as required. In Example 3, the value Y=138573 is represented in base 35 as
3848, which is larger than 1278 as required.