根数的质因数

发布于 2025-01-10 15:02:14 字数 607 浏览 1 评论 0原文

我已经设法用以下代码找到了一个数字的素因数:

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

但是,我正在寻找的是表示简化的根号。例如

在此处输入图像描述

表示为

在此处输入图像描述

在这种情况下,只需获取部首之外的因子即可,然后将它们相乘。相同的程序将适用于给出留在根部内部的数字的因子,

非常感谢您的帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

徒留西风 2025-01-17 15:02:14

这将返回一个表达式作为输出:

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, ")"))
      }
    }
  }
}

示例用法:

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

为了解释该函数,我将逐步介绍给出的前两个示例。

  1. sqrt(12):来自 fac <- gmp::factorize(12) 的因子是 2, 2, 3。来自 unq <- gmp::factorize(12) 的唯一因子。 - unique(fac) 为 2 和 3。n <- tabulate(match(fac, unq)) 返回每个唯一因素出现的次数,分别为 2 和1.对于 r = 2,我们取第二个(平方)根,因此每 2 次出现唯一因子(由商 n %/% r = c(2, 1) 给出) %/% 2 = c(1, 0)),我们可以将它从部首中取出来。将从根式中取出的所有因数相乘即可得到外部数字:mult <- prod(unq^(n %/% r)) = 2^1*3^0 = 2 。类似地,求余运算给出了部首内保留的每个唯一因子的计数:n %% r = c(2, 1) %% 2 = c(0, 1)。将部首内部的所有因数相乘即可得到内部数字:rad <- prod(unq^(n %% r)) = 2^0*3^1 = 3。
  2. 120^(1/3):来自 fac <- gmp::factorize(120) 的因子为 2, 2, 2, 3, 5。独特的因子来自 unq <- unique(fac) 为 2, 3, 5。n <- tabulate(match(fac, unq)) 返回每个的次数这出现唯一因数,分别为 3、1 和 1。对于 r = 3,我们取第三个(立方)根,因此每 3 次出现唯一因数(由商 给出) >n %/% r = c(3, 1, 1) %/% 3 = c(1, 0, 0)),我们可以将它从部首中取出来。乘以从根式中取出的所有因子即可得到外部数字: mult <- prod(unq^(n %/% r)) = 2^1*3^0*5^0 = 2 。类似地,求余运算给出了部首内保留的每个唯一因子的计数:n %% r = c(3, 1, 1) %% 3 = c(0, 1, 1) 。将部首内部的所有因数相乘即可得到内部数字:rad <- prod(unq^(n %% r)) = 2^0*3^1*5^1 = 15

组成代码其余部分的 if 语句用于清晰地格式化返回的表达式。

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.

贱贱哒 2025-01-17 15:02:14

这是一个基本的 R 解决方案

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))
}

,我们可以看到

> 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)

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)
晨光如昨 2025-01-17 15:02:14

我想我已经创造了一些实用的东西。下面的代码是在网上找到的一些想法合并和整理的结果。请原谅代码中的不优雅:

   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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文