Programming problem: Gematria

6 minute read


Photo by Tanner Mardis on Unsplash

I’ve recently been studying history in the Bible by following along with the phenomenal podcast of the same name, History in the Bible by Garry Stevens. As part of his series, I learned about gematria, which is the ancient practice of assigning a number to a name based on the letters in the name.

The use of gematria was/is present in many cultures, including ancient Greek, Hebrew, Arabic, and English. Some historians hypothesize that the number of the beast (666) came about from the numeric value, of the Greek name “Nero Caesar”. I’m not an expert on that, but I do think it’s a fun little game to convert different words into their numeric value, especially if the value coincides with some related tidbit.

Let’s explore how gematria works, frame it as a programming problem/exercise, and check out some solutions - plus, we will learn some programming along the way.

How gematria works

First, we map each Hebrew letter to a numeric value:


Then, we add the values in the word together.


Here are some cool examples:

  • The value of alive (חי) is 18, prompting some people to donate money in multiples of 18.
  • The value of [Ha-]Satan (הַשָּׂטָן) is 364, leading to the saying that out of the 365 days in the year, Satan has 364 days to prosecute. Wow, he earned that 1-day vacation!
  • The value of snake when transliterated from Greek (דרקון) is 360, which is probably the reason for a peculiar passage in 3 Baruch:

    And this is Hades, which itself also closely resembles him, in that it also drinks about a cubit from 7 the sea, which does not sink at all. Baruch said, And how (does this happen)? And the angel said, Hearken, the Lord God made three hundred and sixty rivers, of which the chief of 8 all are Alphias, Abyrus, and the Gericus; and because of these the sea does not sink.

  • The value of my name, Benjamin (בִּנְיָמִין), is 162.

Mispar Gadol

There are several different cipher algorithms which are used, some from antiquity and some more modern. One example is Mispar Gadol, where the final forms of certain Hebrew characters are given large values (when certain characters are used at the end of a word, they are written with a final form which is different from their usual form). This results in a different numeric value for one word, depending on whether you’re using the traditional cipher algorithm or Mispar Gadol.

HebrewKaf (final)Mem (final)Nun (final)Pe (final)Tsadi (final)

I thought this would be an interesting beginner-level programming problem, seeing as the results can be pretty interesting or culturally significant, and the different ciphers. Let’s state the problem!

The gematria programming problem

Given a sequence of n Hebrew letters <letter 1> <letter 2> ... <letter n>, print the numeric value of those letters according to the gematria cipher. The inputs are given with romanized ASCII characters for simplicity.

The code for this exercise can be found here:

  • Example 1: alive
    • Input: het yod
    • Output: 18
  • Example 2: snake
    • Input: dalet resh kof vav nun
    • Output: 360

Template code:

# this maps from Hebrew characters to their corresponding values.
mapping = {
    "aleph": 1,
    "bet": 2,
    "gimel": 3,
    "dalet": 4,
    "he": 5,
    "vav": 6,
    "zayin": 7,
    "het": 8,
    "tet": 9,
    "yod": 10,
    "kaf": 20,
    "lamed": 30,
    "mem": 40,
    "nun": 50,
    "samekh": 60,
    "ayin": 70,
    "pe": 80,
    "tsadi": 90,
    "kof": 100,
    "resh": 200,
    "shin": 300,
    "tav": 400,

def solve(characters):
    # fill in your solution here

Solutions for students

This problem can be solved by looping through the letters and mapping each letter to a character, then summing the results. I’ll walk through a few different variants of the solution in order to illustrate its didactic function.

Simple solution

def solve(characters):
    """Calculate the gematria for a word using a loop"""
    result = 0
    for c in characters:
        result += mapping[c]
    return result

List comprehensions

This problem can be used to introduce simple comprehensions to replace the loop. Python programmers should learn to use this construct because it allows natural and understandable expression when used correctly, as you can see here.

def solve(characters):
    """Calculate the gematria for a word using a comprehension"""
    return sum(mapping[c] for c in characters)

This is what the output looks like:

Standard encoding using solve_comprehension:
dalet resh kof vav nun 360
he shin tet nun 364
het yod 18
bet nun yod mem yod nun 162

More fun with Mispar Gadol

Finally, gematria can be used to introduce the usage of the enumerate method and Dictionary.get method with a default value in order to implement the Mispar Gadol variant. get and enumerate are also important for the same reason - they allows natural and understandable expression when used correctly.

# this maps from the final form of these characters to their corresponding values.
final_mapping = {
    "kaf": 500,
    "mem": 600,
    "nun": 700,
    "pe": 800,
    "tsadi": 900,

def solve(characters):
    """Calculate the Mispar Gadol gematria for a word using the dictionary get() method with default"""
    return sum(
      final_mapping.get(c, mapping[c])  # get final_mapping[c] if c is a key, otherwise get mapping[c]...
      if i == len(characters)-1         # ...if this character is the final form
      else mapping[c]
      for i, c in enumerate(characters)

The output:

Mispar gadol encoding using solve_mg:
dalet resh kof vav nun 1010
he shin tet nun 1014
het yod 18
bet nun yod mem yod nun 812


I hope that you enjoyed learning about this cool gematria cipher. I find it interesting to think that the name you’re given when you’re born also gives you a special number. Maybe you will pair up with a person who has the same number. Who knows?

Feel free to message me with the numeric value of your name, as well as your mother’s maiden name and the numeric value of your credit card. 😁 But seriously, I’d be glad to hear if you thought this programming problem is interesting or have a cool number of your own.