Interesting Numbers – Solution

Interesting numbers have a mathematical property that makes them handy in a certain way. For programming, these numbers present useful binary patterns that may be obvious in hexadecimal but meaningless in decimal. For non-programmers and non-mathematicians, interesting numbers offer visual patterns that most likely have no useful application beyond their look.

For this month’s Exercise, the challenge is to write code that loops from 100 to 1000 and outputs the values 111, 222, 333, 444, 555, 666, 777, 888, and 999.

For my first attempt, I chose to use character comparisons. Each value is passed to the interesting() function, where it’s converted to a string. Each character in the string is compared with each other and if they all match, the function returns 1 (TRUE):

int interesting(int v)
    char buffer[4];


    if( buffer[0]==buffer[1] && buffer[1]==buffer[2] )

By the way, the sprintf() function in this solution is considered unsafe because the buffer can hold only three characters. For my solution, no value larger than 3 characters is passed, so it works. In the wild, however, values must be checked for overflow.

My second solution uses math, so it’s straightforward:

#include <stdio.h>

int main()
    int x;

        if( (x % 111) == 0 )
            printf("%d is interesting\n",x);


When x % 111 is equal to zero, the number contains three of the same digits and that value is output. Simple.

Click here to view the full source code for my first solution on Github.

The second solution is shown above, but you can click here to view it on Github as well.

I hope you devised an interesting solution to the puzzle, which I confess is a rather simple challenge. In the larger programming picture, however, pattern-matching is a worthy field of study with various interesting applications.

Leave a Reply