DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. How Many Ways Can You Tile a Chessboard With Dominoes?

How Many Ways Can You Tile a Chessboard With Dominoes?

When calculating the number of possible combinations, there's some math and, in turn, some code that can help you handle big figures.

John Cook user avatar by
John Cook
·
Oct. 18, 16 · Code Snippet
Like (3)
Save
Tweet
Share
3.60K Views

Join the DZone community and get the full member experience.

Join For Free

suppose you have an n by m chessboard. how many ways can you cover the chessboard with dominoes?

it turns out there’s a remarkable closed-form solution:

\sqrt{\prod_{k=1}^m \prod_{\ell=1}^n \left( 2\cos\left(\frac{\pi k}{m+1} \right) + 2i \cos\left(\frac{\pi \ell}{n+1} \right) \right)}

here are some questions you may have.

but what if n and m are both odd? you can’t tile such a board with dominoes.

yes, in that case, the formula evaluates to zero.

do you need an absolute value somewhere? or a floor or ceiling?

no. it looks like the double product could be a general complex number, but it’s real. in fact, it’s always the square of an integer.

does it work numerically?

apparently so. if you evaluated the product in a package that could symbolically manipulate the cosines, the result would be exact. in floating point, it cannot be, but at least in my experiments, the result is correct when rounded to the nearest integer. for example, there are 12,988,816 ways to tile a standard 8 by 8 chessboard with dominoes, and the following python script returns 12988816.0. for sufficiently large arguments the result will not always round to the correct answer, but for moderate-sized arguments, it should.

from numpy import pi, cos, sqrt

def num_tilings(m, n):
    prod = 1
    for k in range(1, m+1):
    for l in range(1, n+1):
        prod *= 2*cos(pi*k/(m+1)) + 2j*cos(pi*l/(n+1))
    return sqrt(abs(prod))

print(num_tilings(8,8))

the code looks wrong. shouldn’t the ranges go up to m and n ?

no, python ranges are half-open intervals. range(a, b) goes from a to b-1 . that looks unnecessarily complicated, but it makes some things easier.

you said that there was no need for absolute values, but you code has one.

yes, because while, in theory, the imaginary part will be exactly zero, in floating point arithmetic the imaginary part might be small but not zero.

where did you find this formula?

thirty-three miniatures: mathematical and algorithmic applications of linear algebra

Python (language) IT Data Types application

Published at DZone with permission of John Cook, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Mr. Over, the Engineer [Comic]
  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • Distributed Stateful Edge Platforms
  • Cloud-Based Transportation Management System

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: