Skip to content

Instantly share code, notes, and snippets.

@Rudxain
Created March 29, 2023 02:31
Show Gist options
  • Save Rudxain/5abfeeb5e35617677e0c0b23c5dbe02d to your computer and use it in GitHub Desktop.
Save Rudxain/5abfeeb5e35617677e0c0b23c5dbe02d to your computer and use it in GitHub Desktop.
Showcase of math phenomenon where multiples of b-1 having a predictable pattern in base b
#!/usr/bin/env python3
from typing import Final
from string import digits, ascii_letters
from sys import argv
FULL_CHARSET: Final = digits + ascii_letters
def to_radix(n: int, charset='0123456789', lil_endian=False):
'''
convert to base `len(charset)`
'''
radix = len(charset)
out = ''
while True:
if lil_endian:
out += charset[n % radix]
else: # big-endian
out = charset[n % radix] + out
n //= radix
if n == 0:
break
return out
def main(*args: str):
radix: Final = int(args[0]) if len(args) > 0 else 10
if radix < 1 or radix > len(FULL_CHARSET):
raise ValueError(f'unsupported radix: {radix}')
for i in range(radix):
print(to_radix(i * (radix - 1), FULL_CHARSET[:radix]))
if __name__ == '__main__':
main(*argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment