noether/m.py

73 lines
2.2 KiB
Python

# Modulo Test
from prbraid.math import *
from prbraid.color import *
import sys
PROMPT = '[n]: '
def main():
while True:
n = None
strlen_n = None
try:
uprint(PROMPT, color=Color.Blue, end='')
n = input()
strlen_n = len(n)
n = int(n)
except ValueError:
continue
# calculate phi of n
phi = totient(n)
# determine if n is prime
prime = n - 1 == phi
# sys.stdout.write('\x1b[1A')
# sys.stdout.flush()
if prime:
uprint('', moveup=1, end='', flush=False)
column = len(PROMPT) + strlen_n + 1
sys.stdout.write(f'\033[{column}C')
uprint('[PRIME]', color=Color.Magenta, style=Color.Bold, flush=True)
# calculate left padding to align i values
# lpadded = strlen_n
# calculate right padding to align len(orb) values
rpadded = len(str(phi))
# find all invertible elements (skipped for now)
for a in range(n):
orb = orbit(a, n)
ord = len(orb)
# check if `a` is a primitive root
proot = (ord + 1 == n)
# calculate padding
lpad = ' ' * (strlen_n - len(str(a)))
rpad = ' ' * (rpadded - len(str(ord)))
# calculate coloring
color_a = Color.Red
color_ord = None
color_orb = None
style_ord = None
style_orb = None
if proot:
color_a = Color.Green
color_ord = Color.Yellow
color_orb = Color.Green
style_ord = Color.Bold
style_orb = Color.Bold
uprint(f'{lpad}{a}', color=color_a, style=Color.Bold, end=' ', flush=False)
uprint(f'->', end=' ', flush=False)
uprint(f'{ord}{rpad}', color=color_ord, style=style_ord, end=' ', flush=False)
uprint(f'|', end=' ', flush=False)
uprint(f'{orb}', color=color_orb, style=style_orb, flush=True)
print() # empty new line
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, EOFError):
pass