了解分解函数 [英] Understanding factorize function

查看:141
本文介绍了了解分解函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这个问题包含一些破坏因素.

Note that this question contains some spoilers.

问题#12的解决方案指出

除数(包括1和数字本身)可以用质数(和幂)除数中的一个元素来计算."

"Number of divisors (including 1 and the number itself) can be calculated taking one element from prime (and power) divisors."

它执行此操作的(python)代码是num_factors = lambda x: mul((exp+1) for (base, exp) in factorize(x))(其中mul()reduce(operator.mul, ...).)

The (python) code that it has doing this is num_factors = lambda x: mul((exp+1) for (base, exp) in factorize(x)) (where mul() is reduce(operator.mul, ...).)

它没有说明factorize的定义方式,我在理解它的工作方式时遇到了麻烦.它如何告诉您数量因素的数量?

It doesn't state how factorize is defined, and I'm having trouble understanding how it works. How does it tell you the number of factors of the number?

推荐答案

基本思想是,如果将一个因数分解为以下形式,则该形式实际上是标准形式:

The basic idea is that if you have a number factorized into the following form which is the standard form actually:

let p be a prime and e be the exponent of the prime:

N = p1^e1 * p2^e2 *....* pk^ek

现在,要知道N有多少个除数,我们必须考虑素数的每种组合.因此,您可能会说除数为:

Now, to know how many divisors N has we have to take into consideration every combination of prime factors. So you could possibly say that the number of divisors is:

e1 * e2 * e3 *...* ek

但是您必须注意,如果素数因子之一的标准形式的指数为零,那么结果也将为除数.这意味着,我们必须在每个指数上加一个,以确保我们将第i个p包含在零的幂中.这是一个使用数字12的示例-与问题编号:D

But you have to notice that if the exponent in the standard form of one of the prime factors is zero, then the result would also be a divisor. This means, we have to add one to each exponent to make sure we included the ith p to the power of zero. Here is an example using the number 12 -- same as the question number :D

Let N = 12
Then, the prime factors are:
2^2 * 3^1
The divisors are the multiplicative combinations of these factors. Then, we have:
2^0 * 3^0 = 1
2^1 * 3^0 = 2
2^2 * 3^0 = 4
2^0 * 3^1 = 3
2^1 * 3^1 = 6
2^2 * 3^1 = 12

我希望您现在明白为什么在计算除数时为什么要在指数上加一.

I hope you see now why we add one to the exponent when calculating the divisors.

这篇关于了解分解函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆