We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.
User:I am islptng/WIP
Jump to navigation
Jump to search
# -*- coding:utf-8 -*-
from math import gcd
def factorize(n):
factors = {}
if n == 0: return None # 0 is not factorizable
if n == 1: return {} # 1 has no prime factors
i = 2
while i <= n:
if n % i == 0: factors[i] = 0
while n % i == 0:
factors[i] += 1
n //= i
i += 1
return factors
def sup(n):
n = str(n).replace("1","¹").replace("2","²").replace("3","³").replace("4","⁴")
n = n.replace("5","⁵").replace("6","⁶").replace("7","⁷")
n = n.replace("8","⁸").replace("9","⁹").replace("0","⁰")
return n
class TooComplex(Exception): pass # if the calculation is too complex
# (2,5,-7,4,3) -> -2⁴√7³/5i
class spItem:
def __init__(self, num=0, den=1, base=1, root=2, power=1):
self.n,self.d,self.b,self.r,self.p = num,den,base,root,power
self.simplify()
def __str__(self):
if self.d == 0: return "infinity" if self.n else "NaN"
if self.n == 0: return "0"
n,d,b,r,p = self.n,self.d,self.b,self.r,self.p
imaginary = False
if b < 0 & r % 2 == 0:
imaginary = True
b *= -1
res = ""
if n < 0:
res += "-"
n *= -1
if n != 1 or b != 1: res += str(n)
if b != 1:
if r != 2: res += sup(r)
res += "√" + str(b)
if p != 1: res += sup(p)
if d != 1: res += "/" + str(d)
if imaginary: res += "i"
return res
def simplify(self):
if self.d == 0:
if self.n == 0: self.b,self.r,self.p = 1,1,0
if self.n == 0: self.d,self.b,self.r,self.p = 0,1,1,0
if self.p == 0: self.b,self.r = 1,1