Coding For Parents: Roman Numerals

Teach Your Kids About Numbers

Usually kids are only introduced to our 10 based Arabic numerals. It is unfortunate because it can be hard to grasp the concept of numbers when you only learn one system. Often it helps seeing different approaches to understand better the underlying concept.

It why I think it is useful to teach them about alternative ways of dealing with numbers.

Why Learn Roman Numerals

Probably the most common alternative number system to show kids is Roman numerals as they are still used today in various forms e.g. on clocks.

What I like to show to my kids when explaining roman numerals is how they were used in practice, with a roman counting table or abacus. The way we learn for how to perform arithmetic with pen and paper in school, is not very efficient to do with roman numerals. But one thing I’ve learned from investigating old number systems is that you have to understand how they were used. People did not tend to perform elaborate calculations with pen and paper as today, but rather with counting tables or abacus as the one below.

Image for post

With the roman system worked very well, and was quite logical. You see that we got the I, X, C columns. In the I column, every pebble is a 1. In the X column every pebble is a ten. And in the C column every pebble is a hundred. Above we got the V, L and X, which are fives, fifties and five hundred.

With the roman system you basically just write down what the pebbles say. That is quicker than converting to Arabic numerals. Likewise when reading a Roman number it is quick to put that down on a Roman abacus. For this reason Roman numerals were used all the way into the 1500s on Europe, long after Arabic numerals had been introduced.

Anyway, this story is about coding. Lets look at how we can parse roman numerals and turn them into Arabic numerals. Then the kids can try out themselves the numbers and convert them.

Here is an example from the Julia REPL of using the function to parse roman numerals.

Lets have a look at how it works.

Dictionaries

We map or translate the roman letter I, V, X etc to numbers using what is called a dictionary. A dictionary is made up of multiple pairs.

Here is a pair of the letter X and 10.

We can put the pair in a variable pair and examine it.

The dump() function allows us to see which fields the pair is made up of.

We give a list of these pairs to create a dictionary.

The first values in each pair are called the keys in the dictionary, and the second value are called the value. So I, X and C are keys, while 1, 10 and 100 e.g. are values. We can ask a dictionary for the value corresponding to a key like this:

I utilize this dictionary to turn a string of roman letters into an array of regular numbers like this:

This is a fancy way of creating an array in Julia, called a list comprehension. Let me show how it works by setting a value for s

We could have achieved the same with more plain code:

Before we do this lookup we perform the following

That is in case the we wrote the roman letters in lower case letters. Our dictionary relies on the letters being uppercase. We also reverse the order of character because it makes more sense to process them from low to high values.

Conversion

If we consider the roman number XVI, if we reverse it and turn it into numbers it will be [1, 5, 10]. So when e.g. i = 2 then v = 5. If the numbers are in ascending order we just add them up, and get 16 in this case.

However if we process XIV, it will turn into the array [5, 1, 10]. In this case when we reach 1, we get the condition

Which means we subtract from the total value x -= v.

Conclusion

My purpose with these articles about programming for parents is to show how even quite small and simple programs can be useful. Once you know enough programming, it will often be faster to write these programs than to search for a ready made programming doing what you want.

It is a fun exercise to do and doesn’t take that long time. As kids get older you can encourage them to experiment with changing these programs themselves.

Importantly when writing the programs yourself, you have the option to tailor them to your needs. You could e.g. easily modify this to work with the multiplication program from my previous article, to create a program for kids to practice roman numerals and get a score for how many they get correct.

When a ready made program, you are locked down to whatever the creators decided to use it for.

Written by

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store