Prime分解程序的运行速度比正常情况下慢

时间:2018-11-26 10:18:02

标签: c++

我的程序将简单地对数字进行素分解。我觉得这里工作正常,只是有点慢。例如,对于像9999997这样的数字,我没有答案。我认为可以进行一些优化。 我的程序示例: 输入:100 输出:2 ^ 5 * 2 ^ 2 要么 输入:13 输出:13 要么 输入:98 输出:2 ^ 7 * 2

using namespace std;

int getInput();
void caculation(int x);
bool isFirst(int x);

int main() {

  int n = getInput();
  if (isFirst(n) == false && n != 0) {
    caculation(n);
    cout << endl;
  } else {
    cout << n;
    cout << endl;
  }

  return 0;
}
//Functions
int getInput() {
  int x {};
  cin >> x;
  return x;
};
void caculation(int x) {
  bool con = false;
  int j {0};
  int star = 0;
  int sum = x;
  for (int i {sqrt(x)}; sum > 1; i--) {
    if (isFirst(i) == true) {
      while (sum % i == 0) {
        sum = sum / i;
        j++;
        con = true;
      }
      if (con == true) {
        star += 1;
        if (star > 1) cout << "*";
        cout << i;
        if (j != 1) cout << "^" << j;
        j = 0;
        con = false;
      }
    }
  }

}
bool isFirst(int x) {
  if (x <= 1) return false;
  int h = 1;
  for (int i = 2; i <= x; i++) {
    if (x % i == 0) {
      h++;
      if (h > 2) return false;
    }
  }
  return true;
}

2 个答案:

答案 0 :(得分:0)

看看这个实现。 std :: map用于存储因子数据。 例如:6860 = 2 ^ 2 * 5 * 7 ^ 3将显示为映射:{{2,2},{5,1},{7,3}},其中key为素数因子,value为因子度。

此程序的运行速度比您的程序快得多,它会立即分解数字9999999997。

#include <map>
#include <string>
#include <iostream>
#include <cstdint>

using namespace std;

std::map<int64_t, int64_t> decomp(int64_t n)
{
    std::map<int64_t, int64_t> factors;

    int64_t z = 2;
    while(z * z <= n)
        if(n % z == 0)
        {
            factors[z]++;
            n /= z;
        }
        else
            z++;

    if(n > 1)
        factors[n]++;

    return factors;
}

void print_factors(std::map<int64_t, int64_t> factors)
{
    string ch = "";
    for(auto a : factors)
    {
        if(a.second > 1)
            cout << ch << a.first << "^" << a.second;
        else
            cout << ch << a.first;
        ch = "*";
    }
}

int main()
{
    int64_t n = getInput();
    auto factors = decomp(n);
    print_factors(factors);
}

答案 1 :(得分:-1)

我无法遵循您的算法。这是一个解决方案,可以处理较大的示例,并且处理速度更快。该算法的解释在注释中。如有任何问题,请告诉我。

// Program to print all prime factors
# include <stdio.h>
# include <math.h>
#include <iostream>

int getInput() {
    int x {};
    std::cin >> x;
    return x;
};

void PrintPrimeFactors(int n)
{
    int power_count;
    bool factor_found = false;

    // Find home many times n is divisible by 2
    if(n%2 == 0) {

        // count the number of times that n can be divided by 2
        power_count = 1;
        n = n / 2;
        while (n % 2 == 0) {
            n = n / 2;
            power_count++;
        }
        printf("%d^%d", 2, power_count);
        factor_found = true;
    }

    // At this point, if n > 1, then the original n was an odd number.
    // We have already gotten rid of all the 2's, so we can start with i = 3.
    // i represents the starting point to test for more primes (greater than 2)
    // At this point, we know that n is odd, so,
    //     we can skip even numbers to get a little bit more efficiency
    for (int i = 3; i*i <= n; i = i+2)
    {
        if(n%i == 0) {
            // count the number of times that n can be divided by i
            power_count = 1;
            n = n / i;
            while (n % i == 0) {
                n = n / i;
                power_count++;
            }

            // we use 'factor_found' to know if we have a prior factor, causing us to 
            // print an '*' prior to this factor
            if(factor_found) {
                printf(" * ");
            }
            printf("%d^%d", i, power_count);
            factor_found = true;

        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2) {
        if(factor_found) {
            printf(" * ");
        }
        printf("%d", n);
    }
}

int main()
{
    int n = getInput();
    PrintPrimeFactors(n);

    return 0;
}