-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello_gen.py
79 lines (68 loc) · 2.62 KB
/
hello_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from random import choice
import sys
def legendre(a, p):
j = (a ** ((p - 1) // 2)) % p
if j == p - 1:
return -1
else:
return j
def prime(i, primes):
for prime in primes:
if not i % prime:
return False
return True
def primes(n):
primes = set()
for p in range(3, n + 1, 2):
if prime(p, primes):
primes.add(p)
return list(primes)
def make_prog(ins, out):
seen = set()
prime_list = primes(200)
i300 = set(range(1, 300))
code = ""
for i in ins:
tried = set()
next_prime = choice(prime_list)
next_num = next(iter(i300 - seen))
while legendre(next_num, next_prime) != i:
if i == 0:
prime_multiples = {next_prime * i for i in range(2, 10)}
next_num = next(iter(prime_multiples - seen - tried))
else:
next_num = next(iter(i300 - seen - tried))
tried.add(next_num)
seen.add(next_num)
out.write(f"({next_num}/{next_prime})")
return code
# ALTERNATE VERSION THAT ALWAYS FINDS A SOLUTION:
#
# def make_prog(ins, out):
# seen = set()
# prime_list = primes(200)
# ints = set(range(1, len(ins) ** 2))
# for i in ins:
# tried = set()
# next_prime = choice(prime_list)
# next_num = choice(list(ints - seen))
# while next_num in seen | tried or legendre(next_num, next_prime) != i:
# tried.add(next_num)
# next_num += 1
# seen.add(next_num)
# out.write(f"({next_num}/{next_prime})")
# you can translate any program
hello_world = [-1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1,
-1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1,
1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, 1,
-1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1,
-1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1,
1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
-1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1,
-1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1,
-1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1,
1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1,
1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1,
-1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1,
1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1]
print(make_prog(hello_world, sys.stdout))