Math/CSCI 4110/5110 Number Theory

Spring 2017

SageMathCell



For the Division Algorithm:
@interact
def _(a=10,b=2):
    q,r=divmod(a,b)  # does the division algorithm
    print "q=",q,"r=",r
For encoding text as a number:
@interact
def encode(s=""):
    print "Encoding string to a number."
    a=0
    b=ord("~")-ord(" ")+1  # base of our number
    for x in s:
        a=a*b+ord(x)-ord(" ")
    print a
For decoding a number as text:
@interact
def decode(a=0):
    print "Decoding a number to a string."
    s=""
    b=ord("~")-ord(" ")+1  # base of our number
    while a>0:
        q,r=divmod(a,b)
        a=q
        s=chr(r+ord(" "))+s
    print s