eml.py

Created by schraf

Created on April 15, 2026

1.69 KB


import math

a = 0.915965594177
TARGETS = {
 'ln'   : math.log(a),
 'cos'  : math.cos(a),
 'sin'  : math.sin(a),
 'sqrt' : math.sqrt(a),
 '-a'  : -a,
 'exp'  : math.exp(a),
 'a^2'  : a ** 2,
 '1/a'  : 1 / a,
 'ln(ln a)': math.log(-math.log(a)),
 'pi'   : math.pi,
 '0'    : 0.0,
 '1'    : 1.0,
 '2'    : 2.0,
 'e'    : math.e,
 '-1'   : -1,
 'x/2'  : a / 2,
}

class Leaf:
 def __init__(self, name, value):
  self.name  = name
  self.value = value
  self.size  = 1
 def __repr__(self):return self.name
 def eval(self): return self.value

class EmlNode:
 def __init__(self, left, right):
  self.left  = left
  self.right = right
  self.size  = 1 + left.size + right.size

 def __repr__(self): return "E{}{}".format(self.left, self.right)

 def eval(self):
  x = self.left.eval()
  y = self.right.eval()
  if x is None or y is None or y <= 0: return None
  try: return math.exp(x) - math.log(y)
  except: return None


LEAVES = [ Leaf("1", 1.0), Leaf("a", a)]

def trees_of_size(n):
 if n == 1:
  for leaf in LEAVES:
   yield leaf
 elif n >= 3:
  for left_size in range(1, n - 1):
   right_size = n - 1 - left_size
   for left in trees_of_size(left_size):
    for right in trees_of_size(right_size):
     yield EmlNode(left, right)

def all_trees(start=1):
 n = start
 while True:
  yield from trees_of_size(n)
  n += 1

def find_matches(targets=TARGETS, max_size=7, tol=1e-6):
 found = set()
 for n in range(1, max_size + 1):
  for tree in trees_of_size(n):
   val = tree.eval()
   if val is None: continue
   if abs(val) > 10: continue
   for key, target in targets.items():
    if key in found: continue
    if abs(val - target) < tol:
     print(tree, "=", key)
     found.add(key)

find_matches(max_size=20, tol=1e-6)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.