根数的质因数
我已经设法用以下代码找到了一个数字的素因数:
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
be expressed as
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将返回一个表达式作为输出:
示例用法:
UPDATE
为了解释该函数,我将逐步介绍给出的前两个示例。
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。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:
Example usage:
UPDATE
To explain the function, I'll step through the first two examples given.
sqrt(12)
: The factors fromfac <- gmp::factorize(12)
are 2, 2, 3. The unique factors fromunq <- 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. Withr = 2
, we are taking the second (square) root, so every 2 times the unique factors occur (given by the quotientn %/% 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
.120^(1/3)
: The factors fromfac <- gmp::factorize(120)
are 2, 2, 2, 3, 5. The unique factors fromunq <- 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. Withr = 3
, we are taking the third (cube) root, so every 3 times the unique factors occur (given by the quotientn %/% 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.这是一个基本的 R 解决方案
,我们可以看到
Here is a base R solution
and we can see
我想我已经创造了一些实用的东西。下面的代码是在网上找到的一些想法合并和整理的结果。请原谅代码中的不优雅:
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: