GithubHelp home page GithubHelp logo

arithmetic-meme's Introduction

arithmetic-meme

This repo contains a solution in prolog to a simple arithmetic puzzle that went viral-enough on the internet that it popped up on reddit or hackernews or somewhere.

This puzzle seemed like the perfect excuse to write my first Prolog program.

Problem statement

Here are the relevant bits of the problem statement, copy/pasted from here.

All you need to do is place the digits from 1 to 9 in the the grid. Easy, right?

puzzle

...

You need to fill in the gaps with the digits from 1 to 9 so that the equation makes sense, following the order of operations - multiply first, then division, addition and subtraction last.

Running the code

Example SWI-Prolog session

?- [solve].
true.

?- run_tests.
% PL-Unit: solve ................................................................................................................................................................................................................................................................................................ done
% All 288 tests passed
true.

?- solutions(X).
X = [3, 9, 2, 8, 1, 5, 6, 7, 4] ;
X = [3, 9, 2, 8, 1, 5, 7, 6, 4] ;
X = [8, 9, 2, 3, 1, 5, 6, 7, 4] ;
X = [8, 9, 2, 3, 1, 5, 7, 6, 4] .

Example lisp session

CL-USER> (load "solve")
T
CL-USER> (run-tests)
T
CL-USER> (solve)
...list of solutions...

Runtime comparisons

Here is a table showing the runtimes of the various solutions when run on my aging laptop. They range from "pretty fast" when using the clpfd library on SICStus to "dog slow" when using the clpq library on SWI Prolog. In fairness, part of the reason the SWI Prolog solutions are slow is that I still have no idea how to write Prolog.

variant (time units are seconds) SWI Prolog SICStus
normal_precedence_constraint_clpfd 6.236 0.150
linear_precedence_constraint_clpfd 3.360 0.060
normal_precedence_constraint_clpq 62.731 12.230
linear_precedence_constraint_clpq 63.987 12.690

This repo also contains a reference solution in lisp. The lisp solution is a brute-force search over the 9! possible input permutations, and makes no attempt to be fast.

variant (time units are seconds) SBCL CLISP CCL
normal-precedence-constraint-satisfied-p 0.596 2.12 0.977
linear-precedence-constraint-satisfied-p 0.722 3.13 1.217

arithmetic-meme's People

Contributors

appleby avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.