0% found this document useful (0 votes)
770 views2 pages

Send + More Money

This document describes a classic cryptarithmetic problem called SEND + MORE = MONEY. It can be formulated as an equality constraint between the letters represented as digits, along with 28 disequality constraints to ensure each letter is a unique digit. Clojure code is provided that uses finite domain constraints to model and solve this problem.

Uploaded by

Oscar Riveros
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
770 views2 pages

Send + More Money

This document describes a classic cryptarithmetic problem called SEND + MORE = MONEY. It can be formulated as an equality constraint between the letters represented as digits, along with 28 disequality constraints to ensure each letter is a unique digit. Clojure code is provided that uses finite domain constraints to model and solve this problem.

Uploaded by

Oscar Riveros
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

mx-clojure

SEND + MORE = MONEY


Logic and Constraint Programming on Clojure Oscar Riveros

This is a classic example of a so-called cryptarithmetic problem. These are mathematical puzzles in which the digits are replaced by letters of the alphabet or other symbols. The problems dealing with valid sums are called alphametic problems. In the problem under consideration we are asked to replace each letter by a different digit so that the above sum, that is
SEND +MORE MONEY

Is correct. Here the variables are S,E, N, M ,O, R,Y . Because S and M are the leading digits, the domain for each of the consist of the integer interval [1..9 ] . The domain of each of the remaining variables consist of the integer interval [1..9 ] . This problem can be formulated as the equality constraint
1000S + 100E + 10N + D 1000M + 100O + 10R + E = 10000M + 1000O + 100N + 10E + Y

Combined with 28 disequality constraints x y for x, y ranging over the set


{S,E, N, M ,O, R,Y } whit x preceding y in, say, the presented order.
(ns cpcl.smm (:refer-clojure :exclude [==]) (:use [clojure.core.logic.protocols] [clojure.core.logic :exclude [is] :as l]) (:require [clojure.core.logic.fd :as fd]) (:require [clojure.pprint :as pp])) (defn smm [] (run* [q] (fresh [s e n d m o r y] (== q [s e n d m o r y])

mx-clojure
(fd/in s e n d m o r y (fd/interval 0 (fd/distinct [s e n d m o r y]) (fd/!= m 0) (fd/!= s 0) (fd/eq (= (+ (* 1000 s) (* 100 (* 1000 m) (* 100 (+ (* 10000 m) (* 1000 o) (* 100 (smm) 9))

e) (* 10 n) d o) (* 10 r) e) n) (* 10 e) y))))))

#'cpcl.smm/smm ([9 5 6 7 1 0 8 2])

You might also like