prime factors of a radical number

Viewed 101

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

3 Answers

Here is a base R solution

f <- function(n, r) {
  k <- 2
  v <- c()
  while (n >= k) {
    if (!(n %% k)) {
      v <- c(v, k)
      n <- n / k
    } else {
      k <- k + 1
    }
  }
  d <- table(v)
  z <- as.integer(names(d))
  if (all(d %% r == 0)) {
    return(as.expression(prod(z^(d %/% r))))
  }
  if (all(d %/% r == 0)) {
    return(str2lang(sprintf("%s^(1/%s)", prod(z^(d %% r)), r)))
  }
  str2lang(sprintf("%s*%s^(1/%s)", prod(z^(d %/% r)), prod(z^(d %% r)), r))
}

and we can see

> f(1024, 4)
4 * 4^(1/4)

> f(24, 2)
2 * 6^(1/2)

> f(24, 3)
2 * 3^(1/3)

> f(23, 2)
23^(1/2)

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.

I think I have created something functional. The following code is the result of merging and organizing some ideas found on the web. Excuse the inelegance in the code:

   library(exams)
   library(gmp)
   library(dplyr)
   library(plyr)

   typ <- match_exams_device()
   #--------------------------------------------------------
   pipadf <- count(as.numeric(factorize(10)))
   pipadf <- pipadf[order(pipadf$freq), ]
   pipadf
   pipa <- as.numeric(factorize(10))
   pipa
   kuantos <- length(pipa)
   kuantos <- kuantos + 1
   kuantos
   pases <- length(pipadf$freq)
   pases
   x <- 1
   nuevovektor <- vector()

   while(x <= pases){
   unovek <- rep(pipadf[x,1],pipadf[x,2])
   nuevovektor <- c(nuevovektor,unovek)
   x <- x+1
   }

   nuevovektor <- c(nuevovektor,1)
   nuevovektor

   i <- 1
   fderaiz <- vector()
   dderaiz <- vector()

   while(i <= kuantos-1){
   if(nuevovektor[i]==nuevovektor[i+1]){
   x <- nuevovektor[i]
   fderaiz <- c(fderaiz, x)
   i <- i+2
   }else {
    x <- nuevovektor[i]
    dderaiz <- c(dderaiz, x)
    i <- i+1
   }
   }
 
   fderaiz
   dderaiz

   sinrad <- prod(fderaiz)
   sinrad

   conrad <- prod(dderaiz)
   conrad
Related