Skip to content

Instantly share code, notes, and snippets.

@haxelion
Created February 22, 2018 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haxelion/076dad1221b792d7506ebf5be2e3a017 to your computer and use it in GitHub Desktop.
Save haxelion/076dad1221b792d7506ebf5be2e3a017 to your computer and use it in GitHub Desktop.
import math
from multiprocessing import Pool
def owf(input):
output = ""
input = input*input*input
key = str(input*3)
exprime = ["2","3","5","7"]
input = list(str(input))
for index, digit in enumerate(input):
if digit in exprime:
output = key[index]+output
else:
output += key[index]
return output
def walk(left, right, comp):
out = ""
while len(left) + len(right) != 0:
if len(left) == 0:
out += right
break
if len(right) == 0:
out += left
break
else:
if comp(int(left[0]), int(right[0])):
out += left[0]
left = left[1:]
else:
out += right[0]
right = right[1:]
return out
def bruteforce(start, end, target):
print('bruteforce({}, {}, {})'.format(start, end, target))
candidate = start
while candidate < end:
if owf(candidate) == target:
print('Found: {}'.format(candidate))
return True
candidate += 1
return False
enc = "73740945964519364892687222629632"
pivot = 12
# enc = "1631125645095674458269207"
# pivot = 6
print('Pivot: ' + enc[pivot])
# Truncation test
digit_sum = sum(map(int, enc))
if digit_sum % 3 == 0:
truncated = False
else:
truncated = True
print('Truncated: ' + str(truncated))
# Build K min and max
k_max = enc[pivot] + walk(enc[pivot-1::-1], enc[pivot+1:], lambda l, r: l > r)
k_min = enc[pivot] + walk(enc[pivot-1::-1], enc[pivot+1:], lambda l, r: l < r)
if truncated:
k_max += '8'
k_min += '1'
k_max = int(k_max)
k_min = int(k_min)
print('K max: ' + str(k_max))
print('K min: ' + str(k_min))
i_max = int((k_max/3) ** (1.0/3)) + 1
i_min = int((k_min/3) ** (1.0/3))
print('I max: ' + str(i_max))
print('I min: ' + str(i_min))
p = Pool(None)
block = 1000000
for chunk in range(i_min, i_max, block):
p.apply_async(bruteforce, (chunk, chunk + block, enc), callback=lambda x: p.terminate() if x else None)
p.close()
p.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment