Skip to content

Instantly share code, notes, and snippets.

@i-am-unknown-81514525
Last active February 25, 2024 22:30
Show Gist options
  • Save i-am-unknown-81514525/9a1d0847f53b1674f381eb3e65fd8d1c to your computer and use it in GitHub Desktop.
Save i-am-unknown-81514525/9a1d0847f53b1674f381eb3e65fd8d1c to your computer and use it in GitHub Desktop.
Esoteric Number Generator
import math
import traceback
from typing import Callable
# Normal style
repr_1 = '-~(()<())'
num_repr = {
0: f'({repr_1}>>{repr_1})',
1: f'{repr_1}',
2: f'({repr_1}<<{repr_1})',
3: f'({repr_1}+({repr_1}<<{repr_1}))',
4: f'({repr_1}<<{repr_1}<<{repr_1})',
5: f'(({repr_1}<<{repr_1}<<{repr_1})+{repr_1})',
6: f'(({repr_1}<<{repr_1})*(({repr_1}<<{repr_1})+{repr_1}))',
7: f'(({repr_1}<<{repr_1}<<{repr_1}<<{repr_1})-{repr_1})',
8: f'({repr_1}<<{repr_1}<<{repr_1}<<{repr_1})',
9: f'(({repr_1}+({repr_1}<<{repr_1}))**({repr_1}<<{repr_1}))',
10: f'({repr_1}+(({repr_1}+({repr_1}<<{repr_1}))**({repr_1}<<{repr_1})))'
}
func: dict[str, tuple[Callable[[int, int], int], str]] = {
"add": (lambda x, y: x + y, '({}+{})'),
"sub": (lambda x, y: x - y, '({}-{})'),
"pow": (pow, '({}**{})'),
"mul": (lambda x, y: x * y, '({}*{})'),
"bsl": (lambda x, y: x << y, '({}<<{})'),
# "bsr": (lambda x, y: x >> y, '({}>>{})')
}
# Chain style
# repr_1 = '(-~(()<()))'
# num_repr = {
# 0: f'{repr_1} .__rshift__({repr_1})',
# 1: f'{repr_1} ',
# 2: f'{repr_1} .__lshift__({repr_1})',
# 3: f'{repr_1} .__lshift__({repr_1}).__add__({repr_1})',
# 4: f'{repr_1} .__lshift__({repr_1}).__lshift__({repr_1})',
# 5: f'{repr_1} .__lshift__({repr_1}).__lshift__({repr_1}).__add__({repr_1})',
# 6: f'{repr_1} .__lshift__({repr_1}).__mul__({repr_1} .__lshift__({repr_1}).__add__({repr_1}))',
# 7: f'{repr_1} .__lshift__({repr_1}).__lshift__({repr_1}).__lshift__({repr_1}).__sub__({repr_1})',
# 8: f'{repr_1} .__lshift__({repr_1}).__lshift__({repr_1}).__lshift__({repr_1})',
# 9: f'{repr_1} .__lshift__({repr_1}).__add__({repr_1}).__pow__({repr_1} .__lshift__({repr_1}))',
# 10: f'{repr_1} .__add__(({repr_1}).__lshift__({repr_1}).__add__({repr_1}).__pow__({repr_1} .__lshift__({repr_1})))'
# }
#
# func: dict[str, tuple[Callable[[int, int], int], str]] = {
# "add": (lambda x, y: x + y, '{}.__add__({})'),
# "sub": (lambda x, y: x - y, '{}.__sub__({})'),
# "pow": (pow, '{}.__pow__({})'),
# "mul": (lambda x, y: x * y, '{}.__mul__({})'),
# "bsl": (lambda x, y: x << y, '{}.__lshift__({})'),
# # "bsr": (lambda x, y: x >> y, '{}.__rshift__({})')
# }
cal_diff = lambda x, y: abs(x-y)
quick_mode = True
bsl_complete = False
bsr_complete = False
pow_complete = False
def gen_num(limit:int=1e4000, num:int=0) -> str:
global num_repr, bsl_complete, bsr_complete, pow_complete
completed = False
diff = abs(num)
sqrt = math.isqrt(num)
add_lim = 200
if num == 0 or num in num_repr:
return (num_repr[num])
while not completed:
for name, func_tup in func.items():
if name == 'bsl':
if bsl_complete:
continue
bsl_complete = True
if name == 'bsr' and bsr_complete:
if bsr_complete:
continue
bsr_complete = True
if name == 'pow' and pow_complete:
if pow_complete:
continue
pow_complete = True
add_item = {}
set_num = set(num_repr)
for k0 in sorted(list(num_repr)):
if ((k0 > min(abs(num), 1000)) or (k0 < max(-abs(num), -1000))) and name in ['pow']:
continue
if abs(k0 - num) > abs(diff * 2):
continue
print(name, k0)
dis = limit
if name in ['bsr', 'bsl'] and k0 > 250:
break
if name in ['add', 'sub']:
if name == 'add':
new = num - k0
if new in num_repr:
add_item[num] = func[name][1].format(num_repr[k0], num_repr[new])
completed = True
break
elif name == 'sub':
new = k0 - num
if new < 0:
continue
if new in num_repr:
add_item[num] = func[name][1].format(num_repr[k0], num_repr[new])
completed = True
break
if k0 > add_lim:
continue
if quick_mode:
if name == 'mul':
if k0 > 1000:
new = num//k0
check = set(range(new-10, new+10)) & set_num
for v in check:
value = func[name][0](k0, v)
if value not in num_repr:
add_item[value] = func[name][1].format(num_repr[k0], num_repr[v])
continue
for k1 in sorted(list(num_repr)):
if k1 > add_lim and name in ['add', 'sub']:
break
if abs(k1) > abs(diff * 2):
break
if (k1 < 0) and name in ['pow', 'bsl', 'bsr']:
continue
if (k1 > 20) and name in ['pow']:
break
if (k1 > 10) and name in ['bsr', 'bsl']:
break
if name in ['mul']:
if k1 > sqrt:
break
try:
result = func_tup[0](k0, k1)
except Exception as e:
traceback.print_exc()
continue
if name == 'bsl':
print(name, k0, k1)
# print(f'Attempted {name} with {k0}, {k1}')
if diff > cal_diff(num, result) and result not in add_item and result not in num_repr:
add_item[result] = func_tup[1].format(num_repr[k0], num_repr[k1])
if cal_diff(num, result) > diff:
if cal_diff(num, result) > dis:
break
dis = cal_diff(num, result)
if result == num:
completed = True
break
if completed:
break
num_repr = {**num_repr, **add_item}
if completed:
break
add_lim *= 2
if completed:
return (num_repr.get(num))
try:
limit_len = int(input('Number Size:'))
limit = 10**limit_len
except Exception:
limit = 10**4000
while True:
try:
num = int(input('Number:'))
except Exception:
num = 0
print(gen_num(limit, num))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment