简体   繁体   English

改进/修复质因数分解函数

[英]Improve/fix prime factorization function

I have a function prime factorization, but it works wierdly and I have no idea how to make it right.我有一个函数质因数分解,但它的工作很奇怪,我不知道如何使它正确。 It's expected to print factors through 'x' and write like 2^(power) or 3^(power) if 2's or 3's are reapeating factors.如果 2's 或 3's 是重复因子,则预计通过 'x' 打印因子并写成 2^(power) 或 3^(power)。

MyOutput: 2 >> 22^2 |我的输出:2 >> 22^2 | 6 >> 2 x 3^2 | 6 >> 2 x 3^2 | 8 >> 22^22^3 | 8 >> 22^22^3 | 9 >> 3 x 3^2. 9 >> 3 x 3^2。
How do I change this code to make it work properly.如何更改此代码以使其正常工作。

Note: I have stated in main() that if num == 1: print 1.注意:我在 main() 中说过如果 num == 1: 打印 1。

void prime_factors(int num)
{
    int power = 0;

    for (int factor = 2; num > 1; ++factor)
    {
        while (num % factor == 0)
        {
            if (factor >= 3 && power >= 1)
                printf(" x %d", factor);
            else
                printf("%d", factor);
            num /= factor;
            ++power;
            if (power >= 1)
            {
                printf("^%d", power);
            }
        }
    }
}

There are four problems:有四个问题:

  1. power is not being reset to 0 for each factor每个因素的power不会重置为 0
  2. It is printing factor even if power is 0.即使power为0,它也是打印factor
  3. It should not print the factor and power until power has been fully determined.它不应该打印的factorpower ,直到power已经完全确定。 (Currently, the code is printing every time power is incremented. (目前,每次增加power都会打印代码。
  4. It prints x at the beginning if the first factor is > 2.如果第一个因子 > 2,它会在开头打印x

Fixed version below:固定版本如下:

void prime_factors(int num)
{
    int power = 0;
    int first = 1;

    for (int factor = 2; num > 1; ++factor)
    {
        power = 0;
        while (num % factor == 0)
        {
            num /= factor;
            ++power;
        }
        if (power >= 1)
        {
            if (first)
                printf("%d", factor);
            else
                printf(" x %d", factor);
            printf("^%d", power);
            first = 0;
        }
    }
}

There are various ways to speed it up.有多种方法可以加快速度。

One way to speed it up is to skip factors when they get too large (larger than the square root of num , as suggested by @chux in the comments), leaving num as the only remaining factor.加快速度的一种方法是在因子变得太大时跳过因子(大于num平方根,正如@chux在评论中所建议的那样),将num作为唯一剩余的因子。 Rather than calculating the square root, a simple division can be used, as shown in the // speed up 1 code section below:可以使用简单的除法而不是计算平方根,如下面的// speed up 1代码部分所示:

void prime_factors(int num)
{
    int power = 0;
    int first = 1;

    for (int factor = 2; num > 1; ++factor)
    {
        power = 0;
        // speed up 1
        if (num / factor < factor)
        {
            // skip impossible factors
            factor = num;
        }
        // end of speed up 1
        while (num % factor == 0)
        {
            num /= factor;
            ++power;
        }
        if (power >= 1)
        {
            if (first)
                printf("%d", factor);
            else
                printf(" x %d", factor);
            printf("^%d", power);
            first = 0;
        }
    }
}

Another way to speed it up is to increment factor by 2 in the for loop most of the time, except when factor is 2, so the sequence will be 2, 3, 5, 7, 9, 11, etc.:另一种加快速度的方法是在大多数情况下在for循环中将factor增加 2,除非factor为 2,因此序列将是 2、3、5、7、9、11 等:

    for (int factor = 2; num > 1; factor += 1 + (factor & 1))

The factor += 1 + (factor & 1) increments factor by 1 when factor is even, and increments factor by 2 when factor is odd, so the only even value of factor will be the initial value 2.factor += 1 + (factor & 1)的增量factor 1时factor为偶数时,和增量factor由2时factor是奇数,所以唯一的偶数值factor将所述初始值2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM