begin shift to nim code base

This commit is contained in:
Emile Clark-Boman 2025-06-16 20:47:52 +10:00
parent 33bcffdc69
commit 4b20f9961b
25 changed files with 625 additions and 303 deletions

View file

@ -1,105 +1,34 @@
#!/usr/bin/env python3
import sys
import readline
from noether.cli.style import *
from noether.cli.prompt import *
from noether.lib.structs import Result
class Noether(Prompt):
DEFAULT_PROMPT = style('~>> ', Color.BLUE)
def __init__(self) -> None:
super().__init__()
from noether.math import *
from noether.cli import *
def _parse(self, command: str) -> int:
try:
return Result.succeed(int(command))
except ValueError:
return Result.fail('Not an integer.')
PROMPT = '[n]: '
'''
Pairwise orbit cumulative summation (cum orb)
'''
def orb_cum(cum, orb):
for i in range(len(cum)):
cum[i] += orb[i]
def _exec(self, command: int) -> None:
print(style(f'OMG {command}', Color.CYAN))
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
try:
noether = Noether()
while True:
noether.prompt()
except (KeyboardInterrupt, EOFError):
err = style('Exit Requested...', Effect.ITALICS)
print('\n', style('[!]', Color.RED), err)
# 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 = orbit(a, n)
# check if `a` is a primitive root
proot = (ord + 1 == n)
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
# handles premature SIGINT/EOF
pass