For the online learning world

Elearning WorldTechnical

Challenge accepted!


Recently a mate of mine had been mulling over a puzzle that he’d found via Reddit and couldn’t solve. It goes something like this “You can only be exactly twice someone’s age once, and it’s when they turn the same age you were when they were born. Every birthday after that makes you less than twice their age”. So I thought, ‘Hang on a moment! I’ve been writing calendar orientated small Java programs, Java has a “GregorianCalendar” class that I can use, and you can perform time functions! Challenge accepted!’.

To understand how to install and use Java and Netbeans (in the ‘An unexpected editor’ post) used in this post, please see – www.elearningworld.org/tag/java.


Ubuntu® is a registered trademark of Canonical Ltd – ubuntu.com/legal/intellectual-property-policy.

Other names / logos can be trademarks of their respective owners. Please review their website for details.

I am independent from the organisations mentioned and am in no way writing for or endorsed by them.

The information presented in this article is written according to my own understanding, there could be technical inaccuracies, so please do undertake your own research.


The thought process

In visualising the problem it appears that there is a doubling going on, the ‘twice’, so as a persons age increments by one then that is doubled giving an ‘answer’. Your age though only increments by one, so when comparing the two then indeed the ‘answer’ will initially be below your age (you’re greater than twice their age), then equal, then greater than (you are less than twice their age). In my mind I had two vertical lines along a timeline, one for each person and as time progressed, one was behind, then at the same point, then beyond the other – the ‘doubling’ or ‘twice’.

There is also the element ‘it’s when they turn the same age you were when they were born’, and so that means that they are younger than you and the starting point is the date they’re born.

Now that I had a thought of what should be going on it was time to represent that in code, where elements represent parts of the thought.

The code

public class DoubleAge {

    private final GregorianCalendar ageOne = new GregorianCalendar();
    private final GregorianCalendar ageTwo = new GregorianCalendar();
    private final GregorianCalendar current = new GregorianCalendar();

    private final int maxYears = 20;

     * @param args the command line arguments
    public static void main(String[] args) {
        DoubleAge us = new DoubleAge();

    public DoubleAge() {
        this.ageOne.set(1968, 1, 24); // Birthday of older person.
        this.ageTwo.set(1980, 1, 12); // Birthday of younger person.
        this.current.set(1980, 1, 12); // Start point being the birthday of the younger person.

    public void ages() {
        int yearCount = 0;
        int oneAge;
        int twoAge;
        int dble;
        while (yearCount < this.maxYears) {
            oneAge = this.ourAge(this.ageOne);
            twoAge = this.ourAge(this.ageTwo);
            System.out.print("One's age: " + oneAge);
            System.out.print(" Two's age: " + twoAge);
            dble = twoAge * 2;
            System.out.print(" Double two's age: " + dble + " - ");
            if (dble == oneAge) {
            } else if (dble < oneAge) {
                System.out.println((oneAge - dble) + " years more than double");
            } else if (dble > oneAge) {
                System.out.println((dble - oneAge) + " years less than double");
            this.current.add(GregorianCalendar.YEAR, 1);


    private int ourAge(GregorianCalendar theOne) {
        int ourAge = this.current.get(GregorianCalendar.YEAR) - theOne.get(GregorianCalendar.YEAR);
        if (this.current.get(GregorianCalendar.DAY_OF_YEAR) < theOne.get(GregorianCalendar.DAY_OF_YEAR)) {
        return ourAge;

To start we establish the number of years we will iterate over and the birthdays (made up) of the persons involved, ‘ageOne’ being the older and ‘ageTwo’ being the younger. Then we set the ‘current’ date to be that of the birthday of the younger person.

Next we have a helper method ‘ourAge’ that works out how old the given person is in relation to the ‘current’ date. It does this by subtracting the current year from the birth year of the person. If the day in the current year is less than the birth ‘day’ of the person then that means they have not reached that age within the current year yet, so we subtract a year.

Now that we have the method ‘ourAge’ to work out the ages of the two persons, we can iterate year by year and ‘double’ the age of the second person ‘ageTwo’ as we go. That ‘double’ we can then compare against the age of the older person ‘ageOne’ and determine if it is less, equal or greater than. With that we then can calculate how many years the older person (being ‘You’ in the puzzle) is either more, equal or less than the ‘doubled age’ of the younger person.

The output

When we run the program in Apache Netbeans on Ubuntu, then we get:

One's age: 11 Two's age: 0 Double two's age: 0 - 11 years more than double
One's age: 12 Two's age: 1 Double two's age: 2 - 10 years more than double
One's age: 13 Two's age: 2 Double two's age: 4 - 9 years more than double
One's age: 14 Two's age: 3 Double two's age: 6 - 8 years more than double
One's age: 15 Two's age: 4 Double two's age: 8 - 7 years more than double
One's age: 16 Two's age: 5 Double two's age: 10 - 6 years more than double
One's age: 17 Two's age: 6 Double two's age: 12 - 5 years more than double
One's age: 18 Two's age: 7 Double two's age: 14 - 4 years more than double
One's age: 19 Two's age: 8 Double two's age: 16 - 3 years more than double
One's age: 20 Two's age: 9 Double two's age: 18 - 2 years more than double
One's age: 21 Two's age: 10 Double two's age: 20 - 1 years more than double
One's age: 22 Two's age: 11 Double two's age: 22 - Double!
One's age: 23 Two's age: 12 Double two's age: 24 - 1 years less than double
One's age: 24 Two's age: 13 Double two's age: 26 - 2 years less than double
One's age: 25 Two's age: 14 Double two's age: 28 - 3 years less than double
One's age: 26 Two's age: 15 Double two's age: 30 - 4 years less than double
One's age: 27 Two's age: 16 Double two's age: 32 - 5 years less than double
One's age: 28 Two's age: 17 Double two's age: 34 - 6 years less than double
One's age: 29 Two's age: 18 Double two's age: 36 - 7 years less than double
One's age: 30 Two's age: 19 Double two's age: 38 - 8 years less than double
The output

And we have our answer!


Now that we’ve understood the process in solving the puzzle, then we can consider how it was solved. The human interpreted the puzzle, analysed it, broke it down into more manageable parts but then configured the machine to undertake the repetitive element of iterating through the years to produce and compare the data, handing over the result to the human to check that it actually does solve the puzzle.

Could or should AI be able to do this? Or should it be that the human does what they are good at and leave the boring iteration to the computer?

In typing this post I realised that the puzzle could also be illustrated in a spreadsheet as that can deal with dates, calculations and visualisation of data. And so in LibreOffice Calc I’ve come up with a solution where you can interpret the line charts to find the cross over point where younger persons double age is the same as the older persons age:

The answer using a spreadsheet with line charts.
The spreadsheet

And indeed what that age is.


Please do say what you think in the comments.

Gareth Barnard
Latest posts by Gareth Barnard (see all)

Gareth Barnard

Gareth is a developer of numerous Moodle Themes including Essential (the most popular Moodle Theme ever), Foundation, and other plugins such as course formats, including Collapsed Topics.

One thought on “Challenge accepted!

  • Fascinating !
    I sometimes wish my mind worked like this – but it just doesn’t, lol.
    It reminds me of those exam questions that start “A train leaves London at 8.00am and another train leaves Birmingham at 9.00am …”


Add a reply or comment...