rprimesprime-factoring

prime factors of a radical number


I have managed to find the prime factors of a number with the following code:

   library(gmp)
   typ <- match_exams_device()
   #--------------------------------------------------------
   pipa <- as.numeric(factorize(25))
   pipa

But, what I am looking for is to represent the simplified radical number. For example

enter image description here

be expressed as

enter image description here

In this case, it would be enough to obtain the factors that remain outside the radical, and then multiply them. The same procedure would be applied for the factors that give as a result the number that remains inside the radical

Thank you very much for your help


Solution

  • This will return an expression as the output:

    radical_simplify <- function(x, r) {
      fac <- as.integer(gmp::factorize(x))
      unq <- unique(fac)
      n <- tabulate(match(fac, unq))
      rad <- prod(unq^(n %% r))
      if (rad == 1L) {
        parse(text = prod(unq^(n %/% r)))
      } else {
        mult <- prod(unq^(n %/% r))
        if (r == 2L) {
          if (mult == 1L) {
            parse(text = paste0("sqrt(", rad, ")"))
          } else {
            parse(text = paste0(mult, "*sqrt(", rad, ")"))
          }
        } else {
          if (mult == 1L) {
            parse(text = paste0("", rad, "^(1/", r, ")"))
          } else {
            parse(text = paste0(mult, "*", rad, "^(1/", r, ")"))
          }
        }
      }
    }
    

    Example usage:

    radical_simplify(12L, 2L)
    #> expression(2*sqrt(3))
    radical_simplify(120L, 3L)
    #> expression(2*15^(1/3))
    identical(9375^(1/5), eval(radical_simplify(9375, 5)))
    #> [1] TRUE
    

    UPDATE

    To explain the function, I'll step through the first two examples given.

    1. sqrt(12): The factors from fac <- gmp::factorize(12) are 2, 2, 3. The unique factors from unq <- unique(fac) are 2 and 3. n <- tabulate(match(fac, unq)) returns the numbers of times each of the unique factors occur, which are 2 and 1. With r = 2, we are taking the second (square) root, so every 2 times the unique factors occur (given by the quotient n %/% r = c(2, 1) %/% 2 = c(1, 0)), we can pull it out of the radical. Multiply all the factors that get pulled out of the radical to get the outside number: mult <- prod(unq^(n %/% r)) = 2^1*3^0 = 2. Similarly, the remainder operation gives the count of each unique factor that remains inside the radical: n %% r = c(2, 1) %% 2 = c(0, 1). Multiply all the factors that remain inside the radical to get the inside number: rad <- prod(unq^(n %% r)) = 2^0*3^1 = 3.
    2. 120^(1/3): The factors from fac <- gmp::factorize(120) are 2, 2, 2, 3, 5. The unique factors from unq <- unique(fac) are 2, 3, 5. n <- tabulate(match(fac, unq)) returns the numbers of times each of the unique factors occur, which are 3, 1, and 1. With r = 3, we are taking the third (cube) root, so every 3 times the unique factors occur (given by the quotient n %/% r = c(3, 1, 1) %/% 3 = c(1, 0, 0)), we can pull it out of the radical. Multiply all the factors that get pulled out of the radical to get the outside number: mult <- prod(unq^(n %/% r)) = 2^1*3^0*5^0 = 2. Similarly, the remainder operation gives the count of each unique factor that remains inside the radical: n %% r = c(3, 1, 1) %% 3 = c(0, 1, 1). Multiply all the factors that remain inside the radical to get the inside number: rad <- prod(unq^(n %% r)) = 2^0*3^1*5^1 = 15.

    The if statements that make up remainder of the code are to cleanly format the expression that gets returned.