Advertisement
Patrolin

Seximal Fractions

Mar 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1.  
  2. from fractions import Fraction
  3. from typing import *
  4.  
  5.  
  6. def rep(a: int, b: int, base: int):
  7.  
  8.     # SETUP
  9.     high = Fraction(a, b)
  10.     low = high % 1
  11.  
  12.     # TOP
  13.     def top():
  14.         nonlocal high
  15.         digits = []
  16.         while high > 0:
  17.             high, c = divmod(high, base)
  18.             digits.append(int(c))
  19.         return list(reversed(digits))
  20.  
  21.     # BOTTOM
  22.     def bottom():
  23.         nonlocal low
  24.         digits, remainders = [], []
  25.         while low > 0:
  26.             low *= base
  27.             digit, remainder = int(low), low  # Optimize later
  28.             try:
  29.                 i = remainders.index(remainder)
  30.             except ValueError:
  31.                 digits.append(digit)
  32.                 remainders.append(remainder)
  33.             else:
  34.                 return digits[:i], digits[i:]
  35.             low %= 1
  36.         return digits, []
  37.  
  38.     # CALC
  39.     return top(), bottom()
  40.  
  41.  
  42. def mml(seq: Sequence[int]):
  43.     return ''.join('rcdefgab'[c] if 0 <= c <= 7 else str(c) for c in seq)
  44.  
  45.  
  46. def yt(seq: Sequence[int]):
  47.     return ''.join(' 67890'[c] if 0 <= c <= 7 else str(c) for c in seq)
  48.  
  49.  
  50. def rows(n: int):
  51.     for i in range(2, n):
  52.         top, bottom = rep(1, i, 6)
  53.         b, r = bottom
  54.         b, r = yt(b), yt(r)
  55.         yield f'{b}{f"({r})" if r else ""}'
  56.  
  57.  
  58. for r in rows(31):
  59.     print(r, end='')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement