Created
March 29, 2023 02:31
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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