noether/py/m.py

107 lines
3.4 KiB
Python

#!/usr/bin/env python3
import sys
import readline
from noether.lib.math import *
from noether.cli import *
PROMPT = '[n]: '
'''
Pairwise orbit cumulative summation (cum orb)
'''
def orb_cum(cum, orb):
for i in range(len(cum)):
cum[i] += orb[i]
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
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)
# primitive root values
proot_v = []
# primitive root count
proot_c = 0
# cumulative sum of primitive root orbits
prorb_cum = []
# 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, ord, periodic = orbit(a, n)
# check if `a` is a primitive root
# NOTE: this should actually be `proot = (ord == phi(n))`
# proot = (ord + 1 == n)
proot = (ord == phi)
proot_c += proot
# 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
proot_v.append(a)
if not prorb_cum:
prorb_cum = orb
else:
orb_cum(prorb_cum, orb)
elif gcd(a, n) == 1:
color_a = Color.Yellow
uprint(f'{lpad}{a}', color=color_a, style=Color.Bold, end=' ', flush=False)
uprint('->', end=' ', flush=False)
uprint(f'{ord}{rpad}', color=color_ord, style=style_ord, end=' ', flush=False)
uprint('|', end=' ', flush=False)
uprint(f'{orb}', color=color_orb, style=style_orb, flush=True)
uprint('Cum Orb: ', end='', color=Color.Cyan)
uprint(f'{prorb_cum}', flush=True)
prorb_cum_mod = [x % n for x in prorb_cum]
uprint(f' {prorb_cum_mod}', flush=True)
uprint('Roots: ', end='', color=Color.Cyan)
uprint(proot_v, flush=True)
root_delta = [proot_v[i+1] - proot_v[i] for i in range(proot_c - 1)]
uprint('Delta: ', end='', color=Color.Cyan)
uprint(root_delta, flush=True)
uprint('Roots/Phi: ', end='', color=Color.Cyan)
uprint(f'{proot_c}/{phi}\n', flush=True)
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, EOFError):
pass