Skip to content

Instantly share code, notes, and snippets.

@tkoz0
Created June 24, 2023 21:07
Show Gist options
  • Save tkoz0/a80965ef70ff1986ebb36189848ca836 to your computer and use it in GitHub Desktop.
Save tkoz0/a80965ef70ff1986ebb36189848ca836 to your computer and use it in GitHub Desktop.
scripts to test reading y-cruncher digit .ycd files
import sys
DIGITS = int(sys.argv[1])
assert DIGITS > 0 and DIGITS <= 6
outfile = open(sys.argv[2],'w')
# use sliding window of digits
offset = 1
window = sys.stdin.read(DIGITS)
assert len(window) == DIGITS
assert type(window) == str
# store offsets, 0 = not found
results = [0] * (10**DIGITS)
results[int(window)] = 1
while True:
nextdigit = sys.stdin.read(1)
if nextdigit == '': break
offset += 1
window = window[1:] + nextdigit
number = int(window)
if results[number] == 0:
results[number] = offset
# write output in "sequence:offset" format
for i in range(10**DIGITS):
outfile.write(('%%0%dd:%%d\n'%DIGITS)%(i,results[i]))
outfile.close()
infile = open(sys.argv[1],'rb')
digits = int(sys.argv[2])
radix = sys.argv[3].lower()
# get past null character that ends the header
while infile.read(1) != b'\x00': pass
# read digits up to limit specified
# does not care about y-cruncher format which may include garbage data as
# padding beyond the end of the digits stored
# if too many digits are specified, it will have an error when reaching eof
if radix == 'dec':
while True:
if digits <= 0: break
value = struct.unpack('<q',infile.read(8))[0]%(2**64)
outstr = '%019d'%value
sys.stdout.write(outstr[:min(19,digits)])
digits -= 19
elif radix == 'hex':
while True:
if digits <= 0: break
value = struct.unpack('<q',infile.read(8))[0]%(2**64)
outstr = '%016x'%value
sys.stdout.write(outstr[:min(16,digits)])
digits -= 16
else: assert 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment