Code Monkey home page Code Monkey logo

math-desolver's Introduction

Math De-Solver

This whimsical Python script takes a single integer as input, and expands it to an equivalent arithmetic expression of a size corresponding to the specified depth ("de-solving it"). The output expression, which simplifies to the input number, is returned in a standard format that can be plugged into a calculator program, as well as in TeX formatting.

Note that the expression tree will not always reach the maximum depth specified due to limitations regarding keeping every operand an integer, as well as avoiding huge numbers that would make the program hang.

Usage

python desolver.py <Input integer> <Max depth of expression tree>

Example output for python desolver.py 42 8:

Standard Format:
(((2 + 2) + (((1694400 / ((224296565760 / (6 * 54386)) / ((78044 * 4) / (208 + 228)))) - ((((10 / 2) + (17 * 2)) * 101) / (3 * ((18876 / 132) / (21 - 10))))) / (2 * ((((190 + 365) + (319 - 130)) / ((244992 / 464) / (132 / 6))) - (((35 / 5) - 2) + (2 + (7 + 1))))))) - (((((11 + (2 * (7 - 2))) * (((14 + 1) / 3) - 2)) / ((((550 / 22) - (9 - 4)) / ((13 - 6) - 3)) - 2)) * (((((19 * 11) / 11) * 2) + 2) / ((((16 - 4) - (20 / 4)) + 2) - (((4 * 10) / (3 + 2)) / 2)))) / ((((((399289 - 186631) / (120 + 81)) + ((7611 + 16899) / (126 + 3))) - (((88 * 3) + (4 * 31)) - (2 * (34 + 22)))) / ((5 - 2) * (((9 + 15) / 3) + 1))) / ((((13 - 2) - 2) - (2 + 1)) / 2))))

TeX Format:
(((2 + 2) + \frac{(\frac{1694400}{\frac{\frac{224296565760}{6 \times 54386}}{\frac{78044 \times 4}{(208 + 228)}}} - \frac{(\frac{10}{2} + 17 \times 2) \times 101}{3 \times \frac{\frac{18876}{132}}{(21 - 10)}})}{2 \times (\frac{((190 + 365) + (319 - 130))}{\frac{\frac{244992}{464}}{\frac{132}{6}}} - ((\frac{35}{5} - 2) + (2 + (7 + 1))))}) - \frac{\frac{(11 + 2 \times (7 - 2)) \times (\frac{(14 + 1)}{3} - 2)}{(\frac{(\frac{550}{22} - (9 - 4))}{((13 - 6) - 3)} - 2)} \times \frac{(\frac{19 \times 11}{11} \times 2 + 2)}{((((16 - 4) - \frac{20}{4}) + 2) - \frac{\frac{4 \times 10}{(3 + 2)}}{2})}}{\frac{\frac{((\frac{(399289 - 186631)}{(120 + 81)} + \frac{(7611 + 16899)}{(126 + 3)}) - ((88 \times 3 + 4 \times 31) - 2 \times (34 + 22)))}{(5 - 2) \times (\frac{(9 + 15)}{3} + 1)}}{\frac{(((13 - 2) - 2) - (2 + 1))}{2}}})

The output expression simplified to 42. The corresponding TeX:


As a more extreme example, by increasing the depth you can create even further comically large expressions, such as this result from running python desolver.py 88 16 (zoom and scrolling required for full effect)

Future Improvements

  • Add more operations/functions
  • Built-in TeX rendering and output

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.