Creating numerical Haiku and Tanka poems with JavaSolver Challenge Nov-2019 asks us to build numerical versions of traditional Japanese haiku poems. Here is an example of a traditional Haiku poem:
(5) The sky is so blue.
(7) The sun is so warm up high.
(5) I love the summer.

A haiku poem consists of three-lines written in a 5/7/5 syllable count. Here is an example of a numerical haiku:
          77    [seventy seven has 5 syllables]
     + 123   [one hundred twenty three has  6 syllables + 1 syllable for “plus”]
     = 200   [two hundred has  3 syllables + 2 syllables for “equals”]

Nathan Brixius proposed a solution using the open-source constraint package written in Python. I really like this problem and decided to ponder this challenge with a free open-sourced JavaSolver. Below is my solution for numerical Haiku and Tanka.

First of all, we need to know how to write the number pronunciations if English and how many syllables are in these names. So, I created a Java class “SyllablesInNumbers” that provides this information using the following methods:

  • public String getName(int number); // returns “seventy seven” for number=77
  • public int getSyllables(int number);  // returns 5 for number=77
  • public int[] getAllSyllables();             // returns an array: array[0]=2; array[1]=1; …; array[77]=5

This class handles integers from 0 to 999, but it can be easily extended to larger numbers.

Then, it became relatively simple to describe our problem in the Java class “Haiku”:

First, in the method define() I created 3 constrained variables X, Y, and Z defined from 0 to the current maximum of numbers in SyllablesInNumbers.

Then I created 3 intermediate constrained variables defined using the JSR-331 element-constraint:

  • sylX represents a number of syllables that corresponds to X (when it will be defined)
  • sylY represents a number of syllables that corresponds to Y
  • sylZ represents a number of syllables that corresponds to Z

Then it was quite natural to post the constrains for each line if our “haiku”.

To solve the problem, my main program uses the standard JavaSolver’s method solveAll() that is supposed to find all solutions.

I am using the method saveSolution() for nice printing of all found solutions. My program quickly found 166 different solutions. Here are the first 7:

A Tanka is another traditional Japanese poem known as a short song that consists of five lines with the 5/7/5/7/7 syllable count. Here is an example:
(5) I love my kitten.
(7) She is so little and cute.
(5) She has a pink tongue,
(7) And lots of long whiskers too.
(7) She purrs when I stroke her back.

So,  I decided to try to build numerical Tankas as well.

I used the same class “SyllablesInNumbers” and described this problem in the Java class “Tanka”:

There are more than 1000 solutions and here are the first 7:

All sources can be downloaded and executed together with a free JavaSolver available from

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.