Final Project (R)

Being able to do arithmetic in Zp will require us to create our set Zp, where p is prime.

ZpGen <- function(){ 
  # First we determine if the input by the user is an integer or not
  p <<- as.integer(readline(prompt="Enter a prime integer: "))
  if(!grepl("^[0-9]+$",p))
  {
    return(ZpGen())
  }
  if(p == 1){
    return(ZpGen())
  }
  False = TRUE 
  for(i in 2:p){
    if (False == TRUE){
      if((p %% i) == 0)
        False = FALSE
    } else {
      return(ZpGen())
    }
  }
  
  #Creating our Field and showing it
  Zp = c(1:p)
  Zp = Zp-1
  cat("Our Field has been selected to be Zp with p =", p, "\n")

  cat("Zp is the set of congruence classes consisting of ", Zp,"\n")
}

ZpMath <- function(){
  
  #Creating the function of adding and multiplying in Zp
  math <- as.character(readline(prompt = "Would you like to 'Add', 'Multiply' or 'Reset' Zp: "))
  if (grepl("[Aa][Dd][Dd]", math) == TRUE){
     one = enterInt()
     two = enterInt()
     three = one + two
     for(i in 1:1000){
       if (three < 0){
        three = three + p
       }
       if (three > 0){
         break
       }
     }
     ans = three %% p
     cat("You selected ",one,"and ",two,", your result by addition is:", one + two, "or in Zp: ",ans,"\n")
  } else
  if (grepl("[Mm][Uu][Ll][Tt][Ii][Pp][Ll][Yy]", math) == TRUE){
     one = enterInt()
     two = enterInt()
     ans = (one*two) %% p
     cat("Your selected ",one,"and ",two,", your result by multiplication is:", one*two, "or in Zp: ",ans,"\n")
  } else
  if (grepl("[Rr][Ee][Ss][Ee][Tt]", math) == TRUE){
    ZpGen()
  } else {
    return(ZpMath())
  }
}

enterInt = function(){
  ans = as.integer(readline(prompt="Enter any integer: "))
    Neg = FALSE
      if(ans < 0){
        ans = ans * -1
        Neg = TRUE
      }
      if(!grepl("^[0-9]+$",ans)){
        return(enterInt())
      } else if(is.integer(ans) == TRUE) {
        if(Neg == TRUE){
          return(-ans)
        } else{
          return(ans)
        }
      } else if(is.integer(as.integer(-ans))){
        return(-ans)
      } else {
        return(enterInt()) 
      }
}

Now we are able to do the Division Algorithm in Q and Zp with our Zp determined by the codes above.

library(polynom)
## Warning: package 'polynom' was built under R version 3.5.3
library(MASS)
enterNum = function(){
  ans = as.numeric(readline(prompt="Enter any rational number: "))
      if(!grepl("^[0-9]+$",ans)){
        return(enterNum())
      } else if(is.integer(ans) == FALSE) {
        return(enterNum())
      } else {
        return(ans)
      }
  
}

DivAlg = function(){
  #Creating the Div Alg for polynomials in Q and Zp
  field = as.character(readline(prompt = "Are you looking to use the Division Algorithm in the Rationals or Zp? "))
  if (grepl("[Rr][Aa][Tt][Ii][Oo][Nn][Aa][Ll][Ss]", field) == TRUE){
     PolyNumCoefs = enterPolyQ()
     PolyNum = polynomial(coef = PolyNumCoefs)
     PolyDenCoefs = enterDenQ()
     PolyDen = polynomial(coef = PolyDenCoefs)
     if (powNum < powDen){
        cat("Your selected the numerator ", as.character(PolyNum)," and the denominator ",as.character(PolyDen),". By the Division Algorithm you have:", 0, "as your qoutient and: ",as.character(PolyNum),"as your remainder.","\n")
     }
     if (powNum == powDen){
       PolyRCoef = PolyNumCoefs-PolyDenCoefs
       PolyR = polynomial(coef = PolyRCoef)
       cat("Your selected the numerator ", as.character(PolyNum)," and the denominator ",as.character(PolyDen),". By the Division Algorithm you have:",1, "as your qoutient and: ",as.character(PolyR)," as your remainder.","\n")
     }
     if (powNum > powDen){
       powDiff = powNum-powDen+1
       powDif = powDiff-1
       PolyQCoef = rep(0,powDif)
       divisAlgList = c(PolyQCoef,PolyDenCoefs)
       PolyQCoef = rep(0,powDiff)
       l=length(divisAlgList)
       PolyRemCoef = rep(0,l)
       for( i in 1:powDiff){
         k = powDiff+1-i
         j = l+1-i
         if (i == 1){
           PolyQCoef[k]=PolyNumCoefs[j]/divisAlgList[j]
           PolyRemCoef = PolyNumCoefs-(PolyQCoef[k]*divisAlgList)
         } else {
           PolyQCoef[k]=PolyRemCoef[j]/divisAlgList[j]
           PolyRemCoef = PolyRemCoef-(PolyQCoef[k]*divisAlgList)
         }
         divisAlgList = cycle(divisAlgList)
       }
       PolyQCoef = as.fractions(PolyQCoef)
       PolyRemCoef = as.fractions(PolyRemCoef)
       PolyQ = polynomial(PolyQCoef)
       PolyRem = polynomial(PolyRemCoef)
       cat("Your selected the numerator ", as.character(PolyNum)," and the denominator ",as.character(PolyDen),". By the Division Algorithm you have:",as.character(PolyQ), "as your qoutient and: ",as.character(PolyRem)," as your remainder.","\n")
     }
  } else if (grepl("[Zz][Pp]", field) == TRUE){
     PolyNumCoefs = enterPolyZp()
     PolyNum = polynomial(coef = PolyNumCoefs)
     cat("You selected ",intCoef1, "as your coefficents in Zp. Your coefficients in Zp for p = ",p," is: ",PolyNumCoefs," Your numerator function is: ",as.character(PolyNum),"\n")
     PolyDenCoefs = enterDenZp()
     PolyDen = polynomial(coef = PolyDenCoefs)
     cat("You selected ",intCoef2, "as your coefficents in Zp. Your coefficients in Zp for p = ",p," is: ",PolyDenCoefs," Your denominator function is: ",as.character(PolyDen),"\n")
     if (powNum < powDen){
        cat("Your selected the numerator ", as.character(PolyNum)," and the denominator ",as.character(PolyDen),". By the Division Algorithm you have:", 0, "as your qoutient and: ",as.character(PolyNum),"as your remainder.","\n")
     }
     if (powNum == powDen){
       PolyRCoef = PolyNumCoefs-PolyDenCoefs
       for (i in 1:powNum){
         if(PolyRCoef[i] < 0){
           PolyRCoef[i] = PolyRCoef[i]+p
         }
       }
       while(polyRCoef[i] < 0){
            polyRCoef[i] = polyRCoef[i] + p
          }
          if(polyRCoef[i] > p){
            polyRCoef[i] = polyCoef[i] %% p
          }
       PolyR = polynomial(coef = PolyRCoef)
       cat("Your selected the numerator ", as.character(PolyNum)," and the denominator ",as.character(PolyDen),". By the Division Algorithm you have:",1, "as your qoutient and: ",as.character(PolyR)," as your remainder.","\n")
     }
     if (powNum > powDen){
       powDiff = powNum-powDen+1
       powDif = powDiff-1
       PolyQCoef = rep(0,powDif)
       divisAlgList = c(PolyQCoef,PolyDenCoefs)
       PolyQCoef = rep(0,powDiff)
       l=length(divisAlgList)
       PolyRemCoef = rep(0,l)
       for( i in 1:powDiff){
         k = powDiff+1-i
         j = l+1-i
         if (i == 1){
           PolyQCoef[k]=PolyNumCoefs[j]/divisAlgList[j]
           PolyRemCoef = PolyNumCoefs-(PolyQCoef[k]*divisAlgList)
         } else {
           PolyQCoef[k]=PolyRemCoef[j]/divisAlgList[j]
           PolyRemCoef = PolyRemCoef-(PolyQCoef[k]*divisAlgList)
         }
         divisAlgList = cycle(divisAlgList)
       }
       PolyQCoef = ZpFix(PolyQCoef)
       PolyRemCoef = ZpFix(PolyRemCoef)
       PolyQ = polynomial(PolyQCoef)
       PolyRem = polynomial(PolyRemCoef)
       cat("Your selected the numerator ", as.character(PolyNum)," and the denominator ",as.character(PolyDen),". By the Division Algorithm you have:",as.character(PolyQ), "as your qoutient and: ",as.character(PolyRem)," as your remainder.","\n")
     }
    } else {
     return(DivAlg())
   }
}

cycle = function(x, n = 1) {
  if (n == 0) {x}
  else {c(tail(x, -n), head(x, n))}
}

enterPolyQ = function(){
  pow1 = as.integer(readline(prompt="Enter the power of your base polynomial: "))
      if(!grepl("^[0-9]+$",pow1)){
        return(enterPolyQ())
      } else if(is.integer(pow1) == FALSE) {
        return(enterPolyQ())
      } else {
        powNum <<- pow1 + 1
        polyCoef1 = rep(0,powNum)
        for(i in 1:powNum){
          polyCoef1[i] = as.numeric(readline(prompt="Enter the next coefficient:"))
        }
        return(polyCoef1)
      }
}

enterDenQ = function(){
  pow2 = as.integer(readline(prompt="Enter the power of your desired polynomial denominator: "))
      if(!grepl("^[0-9]+$",pow2)){
        return(enterDenQ())
      } else if(is.numeric(pow2) == FALSE) {
        return(enterDenQ())
      } else {
        powDen <<- pow2 + 1
        polyCoef2 = rep(0,powDen)
        for(i in 1:powDen){
          polyCoef2[i] = as.integer(readline(prompt="Enter the next coefficient:"))
        }
        return(polyCoef2)
      }
}

enterPolyZp = function(){
  pow1 = as.integer(readline(prompt="Enter the power of your base polynomial: "))
      if(!grepl("^[0-9]+$",pow1)){
        return(enterPolyQ())
      } else if(is.integer(pow1) == FALSE) {
        return(enterPolyQ())
      } else {
        powNum <<- pow1 + 1
        polyCoef1 = rep(0,powNum)
        intCoef1 <<- polyCoef1
        for(i in 1:powNum){
          polyCoef1[i] = as.integer(readline(prompt="Enter the next coefficient as an integer: "))
          intCoef1[i] <<- polyCoef1[i]
          while(polyCoef1[i] < 0){
            polyCoef1[i] = polyCoef1[i] + p
          }
          if(polyCoef1[i] > p){
            polyCoef1[i] = polyCoef1[i] %% p
          }
        }
        return(polyCoef1)
      }
}

enterDenZp = function(){
  pow1 = as.integer(readline(prompt="Enter the power of your desired polynomial denominator: "))
      if(!grepl("^[0-9]+$",pow1)){
        return(enterPolyQ())
      } else if(is.integer(pow1) == FALSE) {
        return(enterPolyQ())
      } else {
        powDen <<- pow1 + 1
        polyCoef2 = rep(0,powDen)
        intCoef2 <<- polyCoef2
        for(i in 1:powDen){
          polyCoef2[i] = as.integer(readline(prompt="Enter the next coefficient as an integer: "))
          intCoef2[i] <<- polyCoef2[i]
          while(polyCoef2[i] < 0){
            polyCoef2[i] = polyCoef2[i] + p
          }
          if(polyCoef2[i] > p){
            polyCoef2[i] = polyCoef2[i] %% p
          }
        }
        return(polyCoef2)
      }
}

getfracs <- function(frac) {
     tmp <- strsplit(attr(frac,"fracs"), "/")[[1]]
     list(numerator=as.numeric(tmp[1]),denominator=as.numeric(tmp[2]))
}

ZpFix = function(x){
  l = length(x)
  x = as.fractions(x)
  NumX = rep(0,l)
  DenX = rep(0,l)
  Statement = FALSE
  for (i in 1:l){
     Fix = getfracs(x[i])
     NumX[i] = Fix$numerator
     DenX[i] = Fix$denominator
  }
  DenX[is.na(DenX)] <- 1
  for (j in 1:l){
    pPrime = p-1
    pList = c(0:pPrime)
    while(Statement == FALSE){
      for(i in 1:p){
        check = (DenX[j]*pList[i]) %% p
        if(check == 1){
          DenX[j] = pList[i]
          break
        }
      }
      Statement = TRUE
    }
    Statement = FALSE
  }
  x = NumX*DenX
  for(i in 1:l){
    while (x[i] < 0){
      x[i] = x[i] + p
    }
    if (x[i] > p){
      x[i] = x[i] %% p
    }
  }
  return(x)
}

From where to need to check to see if there are any roots in Q or Zp.

FindRoots = function(){
  #Now to determine Rationals/Zp to be finding roots in.
  field = as.character(readline(prompt = "Are you looking to find roots in the Rationals or Zp? "))
  if (grepl("[Rr][Aa][Tt][Ii][Oo][Nn][Aa][Ll][Ss]", field) == TRUE){
    PolyCoef = enterPoly()
    QRootsTrue <<- FALSE
    PolyQRoots = QRoots(PolyCoef)
    PolyQRoots = as.fractions(PolyQRoots)
    PolyFunc = polynomial(PolyCoef)
    cat(" You have selected the coefficients of ",PolyCoef,".",
        "\n","This has created the polynomial ",as.character(PolyFunc)," which has the roots: ",PolyQRoots,"\n",
        if(QRootsTrue == FALSE){
          "This result of 0 implies there are no roots in the field of rationals."
        } else {
          "If 0 is listed as a result, it means that 0 is a root in the field of rationals."
        })
  } else if (grepl("[Zz][Pp]", field) == TRUE){
    PolyCoef = enterPoly()
    PolyCoefFixed = ZpFix(PolyCoef)
    ZpRootTrue <<- FALSE
    PolyZpRoots = ZpRoots(PolyCoef)
    PolyZpRoots = PolyZpRoots %% p
    PolyFunc = polynomial(PolyCoefFixed)
    cat(" You have selected the coefficients of ",PolyCoef," which is "
        ,PolyCoefFixed," in Zp.","\n","This has created the polynomial ",
        as.character(PolyFunc)," which has roots: ",PolyZpRoots,"\n",
        if(ZpRootsTrue == FALSE){
          "This result of 0 implies there are no roots in the prime field of Zp."
        } else {
          "If 0 is listed as a result, it means that 0 is a root in the prime field of Zp."
        })
  } else {
    return(FindRoots())
  }
}

enterPoly = function(){
  pow = as.integer(readline(prompt="Enter the power of your polynomial: "))
      if(!grepl("^[0-9]+$",pow)){
        return(enterPoly())
      } else if(is.integer(pow) == FALSE) {
        return(enterPoly())
      } else {
        PolyPow <<- pow + 1
        polyCoef = rep(0,PolyPow)
        for(i in 1:PolyPow){
          polyCoef[i] = as.numeric(readline(prompt="Enter the next coefficient:"))
        }
        return(polyCoef)
      }
}

QRoots = function(x){
  PolyX = polynomial(x)
  PolyXFunc = as.function(PolyX)
  l = length(x)
  DivConstant = divisors(x[1])
  DivLead = divisors(x[l])
  RatioCoef = DivUnion(DivConstant,DivLead)
  NegRatioCoef = RatioCoef * -1
  TestIntegers = union(RatioCoef,NegRatioCoef)
  Testl = length(TestIntegers)
  xRoots <<- rep(0,1)
  for(i in 1:Testl){
    if(PolyXFunc(TestIntegers[i])==0){
      if(xRoots == 0){
      QRootsTrue <<- TRUE
      xRoots <<- TestIntegers[i]
      } else {
      xRoots <<- c(xRoots,TestIntegers[i])
      }
    }
  }
  return(xRoots)
}

divisors = function(x){
  y <- seq_len(abs(x))
  y[ x%%y == 0 ]
}

DivUnion = function(x,y){
  yl = length(y)
  xl = length(x)
  list = 1
  for (j in 1:xl){
    for (i in 1:yl){
      dec = x[j]/y[i]
      list = union(list,dec)
    }
  }
  return(list)
}

ZpRoots = function(x){
  x = ZpFix(x)
  PolyX = polynomial(x)
  FuncX = as.function(PolyX)
  xRoots <<- rep(0,1)
  for (i in 1:p){
    if(FuncX(i)%%p == 0){
      if(xRoots == 0){
      xRoots <<- i
      ZpRootsTrue <<-TRUE
      } else {
      xRoots <<- c(xRoots,i)
      }
    }
  }
  return(xRoots)
}

ZpFix = function(x){
  l = length(x)
  x = as.fractions(x)
  NumX = rep(0,l)
  DenX = rep(0,l)
  Statement = FALSE
  for (i in 1:l){
     Fix = getfracs(x[i])
     NumX[i] = Fix$numerator
     DenX[i] = Fix$denominator
  }
  DenX[is.na(DenX)] <- 1
  for (j in 1:l){
    pPrime = p-1
    pList = c(0:pPrime)
    while(Statement == FALSE){
      for(i in 1:p){
        check = (DenX[j]*pList[i]) %% p
        if(check == 1){
          DenX[j] = pList[i]
          break
        }
      }
      Statement = TRUE
    }
    Statement = FALSE
  }
  x = NumX*DenX
  for(i in 1:l){
    while (x[i] < 0){
      x[i] = x[i] + p
    }
    if (x[i] > p){
      x[i] = x[i] %% p
    }
  }
  return(x)
}

Now let’s use reasonable tests to determine if a polynomial is irreducible or not.

IrreducibleTests = function(){
  #Determine if the function is irreducible.
  field = as.character(readline(prompt = "What field is your polynomial in? Rationals or Zp: "))
  PolyCoef = enterPoly()
  PolyRoots = FindRootsAdj(PolyCoef,field)
  l = length(PolyRoots)
  EisensteinIrreducible = FALSE
  IntRootReducible = FALSE
  PolyIrred = TRUE
  if(PolyIrred == TRUE){
    #First check to see if there are any integer roots, if there are then we know that we can reduce the polynomial
    for (i in 1:l){
      if(is.integer(PolyRoots[i])==TRUE){
        IntRootReducible = TRUE
      }
    }
    #Second check for Eisenstein to see if it's irreducible over the rationals, if it is irreducible over rationals it's irreducible over the integers
    Primes = c(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97)
    lP = length(Primes)
    n = l-1
    test = rep(FALSE,l)
    TrueCounter = 0
    for(i in 1:lP){
      for(j in 1:l){
        if(PolyCoef[j] %% Primes[i] == 0){
          test[j] = TRUE
        }
      }
      for(j in 1:l){
        if(test[j] == TRUE){
          TrueCounter = TrueCounter + 1
        }
        if(TrueCounter == n){
          GoodPrime <<- i
          break
        }
      }
    }
    GoodPrimeSq = GoodPrime**2
    if(test[1] %% GoodPrimeSq != 0){
      if(test[l] %% GoodPrime != 0){
        EisensteinIrreducible = TRUE
      }
    }
  }
  if(IntRootReducible == TRUE){
    cat("We were able to determine that our polynomial does in fact have an integer root. Therefore we know that it is reducible in ",field,".\n")
  } else if(EisensteinIrreducible == TRUE ){
    cat("We were able to determine from the Eisenstein Critereon that our polynomial is not reducible over the rationals.","\n")
  } else {
    cat("We are not sure if the polynomial is irreducible.","\n")
  }
}

FindRootsAdj = function(x, field){
  #Now to determine Rationals/Zp to be finding roots in.
  if (grepl("[Rr][Aa][Tt][Ii][Oo][Nn][Aa][Ll][Ss]", field) == TRUE){
    PolyCoef = x
    QRootsTrue <<- FALSE
    PolyQRoots = QRoots(PolyCoef)
    PolyQRoots = as.fractions(PolyQRoots)
    return(PolyQRoots)
  } else if (grepl("[Zz][Pp]", field) == TRUE){
    PolyCoef = x
    PolyCoefFixed = ZpFix(PolyCoef)
    PolyZpRoots = ZpRoots(PolyCoef)
    return(PolyZpRoots)
  } else {
    fixfield = as.character(readline(prompt = "Are you looking to find roots in the Rationals or Zp? "))
    return(FindRootsAdj(fixfield))
  }
}

enterPoly = function(){
  pow = as.integer(readline(prompt="Enter the power of your polynomial: "))
      if(!grepl("^[0-9]+$",pow)){
        return(enterPoly())
      } else if(is.integer(pow) == FALSE) {
        return(enterPoly())
      } else {
        PolyPow <<- pow + 1
        polyCoef = rep(0,PolyPow)
        for(i in 1:PolyPow){
          polyCoef[i] = as.numeric(readline(prompt="Enter the next coefficient:"))
        }
        return(polyCoef)
      }
}

QRoots = function(x){
  PolyX = polynomial(x)
  PolyXFunc = as.function(PolyX)
  l = length(x)
  DivConstant = divisors(x[1])
  DivLead = divisors(x[l])
  RatioCoef = DivUnion(DivConstant,DivLead)
  NegRatioCoef = RatioCoef * -1
  TestIntegers = union(RatioCoef,NegRatioCoef)
  Testl = length(TestIntegers)
  xRoots <<- rep(0,1)
  for(i in 1:Testl){
    if(PolyXFunc(TestIntegers[i])==0){
      if(xRoots == 0){
      QRootsTrue <<- TRUE
      xRoots <<- TestIntegers[i]
      } else {
      xRoots <<- c(xRoots,TestIntegers[i])
      }
    }
  }
  return(xRoots)
}

divisors = function(x){
  y <- seq_len(abs(x))
  y[ x%%y == 0 ]
}

DivUnion = function(x,y){
  yl = length(y)
  xl = length(x)
  list = 1
  for (j in 1:xl){
    for (i in 1:yl){
      dec = x[j]/y[i]
      list = union(list,dec)
    }
  }
  return(list)
}

ZpRoots = function(x){
  x = ZpFix(x)
  PolyX = polynomial(x)
  FuncX = as.function(PolyX)
  xRoots <<- rep(0,1)
  for (i in 1:p){
    if(FuncX(i)%%p == 0){
      if(xRoots == 0){
      xRoots <<- i
      ZpRootsTrue <<-TRUE
      } else {
      xRoots <<- c(xRoots,i)
      }
    }
  }
  return(xRoots)
}

ZpFix = function(x){
  l = length(x)
  x = as.fractions(x)
  NumX = rep(0,l)
  DenX = rep(0,l)
  Statement = FALSE
  for (i in 1:l){
     Fix = getfracs(x[i])
     NumX[i] = Fix$numerator
     DenX[i] = Fix$denominator
  }
  DenX[is.na(DenX)] <- 1
  for (j in 1:l){
    pPrime = p-1
    pList = c(0:pPrime)
    while(Statement == FALSE){
      for(i in 1:p){
        check = (DenX[j]*pList[i]) %% p
        if(check == 1){
          DenX[j] = pList[i]
          break
        }
      }
      Statement = TRUE
    }
    Statement = FALSE
  }
  x = NumX*DenX
  for(i in 1:l){
    while (x[i] < 0){
      x[i] = x[i] + p
    }
    if (x[i] > p){
      x[i] = x[i] %% p
    }
  }
  return(x)
}

Finally let’s try adjoining an element to the rationals and do some arithmatic with it.

library(polynom)
library(pracma)
## Warning: package 'pracma' was built under R version 3.5.3
## 
## Attaching package: 'pracma'
## The following object is masked from 'package:polynom':
## 
##     integral
AlphaGen <- function(){ 
  # First we ask for the value of n for the n-th root of a designated prime integer
  p <<- as.integer(readline(prompt="Enter a prime integer: "))
  if(!grepl("^[0-9]+$",p))
  {
    return(AlphaGen())
  }
  if(p == 1){
    return(AlphaGen())
  }
  False = TRUE 
  for(i in 2:p){
    if (False == TRUE){
      if((p %% i) == 0)
        False = FALSE
    } else {
      return(AlphaGen())
    }
  }
  
  n <<- as.integer(readline(prompt=cat("Enter the value you wish to be n for the n-th root of ",p,":")))
  if(!grepl("^[0-9]+$",p))
  {
    return(AlphaGen())
  }
  if(n < 2){
    return(AlphaGen())
  }
  if(is.integer(n)==FALSE){
    return(AlphaGen())
  }
  
  zeroes = rep(0,n-1)
  PolyCoefAlpha <<- c(-p,zeroes,1)
  alpha <<- PolyCoefAlpha
  alphaFunc = polynomial(alpha)
  
  cat("Our Algebraic Extension Field of the Rationals has been selected as",print(nthroot(p,n)),"or",p,"to the 1 /",n, "power. For substitution purposes our polynomial will be ",as.character(alphaFunc,decreasing=TRUE),"\n")
  
}

enterPoly = function(){
  alphaPow <<- as.integer(readline(prompt="Enter the power of your polynomial: "))
      if(!grepl("^[0-9]+$",alphaPow)){
        return(enterPoly())
      } else if(is.integer(alphaPow) == FALSE) {
        return(enterPoly())
      } else {
        PolyPow <<- alphaPow + 1
        polyCoef = rep(0,PolyPow)
        for(i in 1:PolyPow){
          polyCoef[i] = as.numeric(readline(prompt="Enter the next coefficient:"))
        }
        return(polyCoef)
      }
}

AlphaMath <- function(){
  
  #Creating the function of adding and multiplying in Zp
  math <- as.character(readline(prompt = "Would you like to 'Add', 'Multiply' or 'Reset' Q(alpha): "))
  if (grepl("[Aa][Dd][Dd]", math) == TRUE){
     one = enterPoly1()
     lOne = length(one)
     oneFunc = polynomial(one)
     two = enterPoly2()
     lTwo = length(two)
     twoFunc = polynomial(two)
     if(lOne > lTwo){
       diffl = lOne-lTwo
       adjoin = rep(0,diffl)
       two = c(two,adjoin)
     }  else if (lOne < lTwo){
       diffl = lTwo-lOne
       adjoin = rep(0,diffl)
       one = c(one,adjoin)
     }
     qAns = one+two
     qAnsFunc = polynomial(qAns)
     ans = ModAlpha(qAns,alpha)
     ansFunc = polynomial(ans)
     cat("You selected ",as.character(oneFunc)," and ",as.character(twoFunc)," with the result by addition being:",as.character(qAnsFunc), "or in Q(alpha): ",as.character(ansFunc),"\n")
    } else if (grepl("[Mm][Uu][Ll][Tt][Ii][Pp][Ll][Yy]", math) == TRUE){
     one = enterPoly1()
     oneFunc = polynomial(one)
     two = enterPoly2()
     twoFunc = polynomial(two)
     oneTwo = PolyMulti(one,two)
     oneTwoFunc = polynomial(oneTwo)
     ans = ModAlpha(PolyMulti(one,two),alpha)
     ansFunc = polynomial(ans)
     cat("You selected ",as.character(oneFunc)," and ",as.character(twoFunc)," with the result by multiplication being:", as.character(oneTwoFunc), "or in Q(alpha): ",as.character(ansFunc),"\n")
    } else if (grepl("[Rr][Ee][Ss][Ee][Tt]", math) == TRUE){
      AlphaGen()
    } else {
    return(AlphaMath())
    }
  }

ModAlpha = function(x,y){
  #DivAlg but for Moding out function
     PolyNumCoefs = x
     PolyAlphaCoefs = y
     xl = length(x)
     yl = length(y)
     powNum = length(x)-1
     powAlpha = length(y)-1
     if (powNum < powAlpha){
       return(x)
     }
     if (powNum == powAlpha){
       n = xl-1
       z = x[1:n]+x[n]*y
       return(z)
     }
     if (powNum > powDen){
       powDiff = powNum-powDen+1
       powDif = powDiff-1
       PolyQCoef = rep(0,powDif)
       divisAlgList = c(PolyQCoef,PolyAlphaCoefs)
       PolyQCoef = rep(0,powDiff)
       l=length(divisAlgList)
       PolyRemCoef = rep(0,l)
       for( i in 1:powDiff){
         k = powDiff+1-i
         j = l+1-i
         if (i == 1){
           PolyQCoef[k]=PolyNumCoefs[j]/divisAlgList[j]
           PolyRemCoef = PolyNumCoefs-(PolyQCoef[k]*divisAlgList)
         } else {
           PolyQCoef[k]=PolyRemCoef[j]/divisAlgList[j]
           PolyRemCoef = PolyRemCoef-(PolyQCoef[k]*divisAlgList)
         }
         divisAlgList = cycle(divisAlgList)
       }
       z = PolyRemCoef
       return(z)
    }
}

enterPoly1 = function(){
  alphaPow <<- as.integer(readline(prompt="Enter the power of your first polynomial: "))
      if(!grepl("^[0-9]+$",alphaPow)){
        return(enterPoly())
      } else if(is.integer(alphaPow) == FALSE) {
        return(enterPoly())
      } else {
        PolyPow <<- alphaPow + 1
        polyCoef = rep(0,PolyPow)
        for(i in 1:PolyPow){
          polyCoef[i] = as.numeric(readline(prompt="Enter the next coefficient:"))
        }
        return(polyCoef)
      }
}

enterPoly2 = function(){
  alphaPow <<- as.integer(readline(prompt="Enter the power of your second polynomial: "))
      if(!grepl("^[0-9]+$",alphaPow)){
        return(enterPoly())
      } else if(is.integer(alphaPow) == FALSE) {
        return(enterPoly())
      } else {
        PolyPow <<- alphaPow + 1
        polyCoef = rep(0,PolyPow)
        for(i in 1:PolyPow){
          polyCoef[i] = as.numeric(readline(prompt="Enter the next coefficient:"))
        }
        return(polyCoef)
      }
}

PolyMulti = function(x,y){
  xl = length(x)
  xpow = xl-1
  yl = length(y)
  ypow = yl-1
  l = (xpow+ypow)+1
  powNew = xpow+ypow
  powdiffx = powNew-xpow
  powdiffy = powNew-ypow
  xzeroes = rep(0,powdiffx)
  yzeroes = rep(0,powdiffy)
  xbase = c(x,xzeroes)
  ybase = c(yzeroes,y)
  productBase = rep(0,l)
  product = rep(0,l)
  lenY = length(ybase)
  for(i in 1:yl){
    k=lenY-yl+i
    productBase = ybase[k]*xbase
    xbase = cycleRev(xbase)
    product = product + productBase
  }
  return(product)
}

cycleRev = function(x, n = 1) {
  if (n == 0) {x}
  else {c(tail(x, n),head(x, -n))}
}