使用筛分法进行素因子分解

时间:2013-06-09 05:27:46

标签: c++ c primes prime-factoring

我的代码出了什么问题? 我试图找到最重要的因素,但在输入崩溃之后它没有用。

我现在该怎么办?我使用单独的方法生成素数后生成素数我只是在main中调用素数函数。

#include<iostream>
#include<cstdio>
#include<math.h>
#define SIZE 1000000
using namespace std;
long p[SIZE],input;
long List[SIZE];  // saves the List
long listSize;   // saves the size of List

void prime(void)
{
       long i,j;
       p[0]=1;
       p[1]=1;
       for(i=2;i<=sqrt(SIZE);i++)  // prime generate part
            if(p[i]==0)
                for(j=2;j*i<=SIZE;j++)
                    p[i*j]=1;
}

void primeFactorize( long n )
{
    listSize = 0;   // Initially the List is empty, we denote that size = 0
    long sqrtN = long( sqrt(n) ); // find the sqrt of the number
    for( long i = 0; p[i] <= sqrtN; i++ ) { // we check up to the sqrt
        if( n % p[i] == 0 ) { // n is multiple of prime[i]
            // So, we continue dividing n by prime[i] as long as possible
            while( n % p[i] == 0 ) {
                n /= p[i]; // we have divided n by prime[i]
                List[listSize] = p[i]; // added the ith prime in the list
                listSize++; // added a prime, so, size should be increased
            }
            // we can add some optimization by updating sqrtN here, since n
            // is decreased. think why it's important and how it can be added
        }
    }
    if( n > 1 )
    {
        // n is greater than 1, so we are sure that this n is a prime
        List[listSize] = n; // added n (the prime) in the list
        listSize++; // increased the size of the list
    }
}
int main()
{
    prime();    
    while(scanf_s("%ld",&input),input)
    {
          if(input==1)
                printf("1 = 1\n");
            else if(input==-1)
                printf("-1 = -1 x 1\n");
            else
            {
                primeFactorize( input );

            }
    for( long i = 0; i < listSize; i++ ) // traverse the List array
                printf("%d ", List[i]);

    }
    return 0;
}

2 个答案:

答案 0 :(得分:1)

prime()例程中,您设置标志以指示数字是否为素数 - 其中1的数字不是素数。但是,在primeFactorize函数中,您假设数组p[]包含素数的值,而不是标志。因此,您很快就会达到除零(因为素数的标志为零),然后崩溃。

您需要确保您访问的阵列具有您期望的数字!

一种可能的方法是创建两个数组:pflagspvalues。当您首次在prime()函数中设置标记时,应该设置pflags。循环遍历所有可能的值后,将数组pflags删除为零值;每次遇到一个时,都会将pvalues的下一个值设置为pflags的索引。像这样:

void prime(void)
{
  long i,j;
  pflags[0]=1;
  pflags[1]=1;
  for(i=2;i<=sqrt(SIZE);i++)  // prime generate part
    if(pflags[i]==0)
      for(j=2;j*i<SIZE;j++)
        pflags[i*j]=1;
  j=0;
  for(i=0; i<SIZE; i++) {
    if(flags[i]==0) pvalues[j++]=i;
  }     
}

当然,您必须正确初始化这些数组,并在pvalues函数中使用p代替primeFactorize。如果你很聪明,你会发现事实上你可以使用相同的数组p来处理这两件事 - 但是要保持你正在做的事情是很棘手的,所以使用两个独立的数组将有助于理解

编辑我决定看看我是否可以运行代码 - 它确实(修复了一个拼写错误,将scanf_s函数更改为scanf并修复后)输出格式从%d%ld)。我也稍微清理了I / O.为了帮助您,我提供了适合我的完整列表:

#include<iostream>
#include<cstdio>
#include<math.h>
#define SIZE 1000000
using namespace std;
long pvalues[SIZE], pflags[SIZE], input;
long List[SIZE];  // saves the List
long listSize;   // saves the size of List


void prime(void)
{
  long i,j;
  pflags[0]=1;
  pflags[1]=1;
  for(i=2;i<=sqrt(SIZE);i++)  // prime generate part
    if(pflags[i]==0)
      for(j=2;j*i<SIZE;j++)
        pflags[i*j]=1;
  j=0;
  for(i=0; i<SIZE; i++) {
    if(pflags[i]==0) pvalues[j++]=i;
  }     
}
void primeFactorize( long n )
{
    listSize = 0;   // Initially the List is empty, we denote that size = 0
    long sqrtN = long( sqrt(n) ); // find the sqrt of the number
    for( long i = 0; pvalues[i] <= sqrtN; i++ ) { // we check up to the sqrt
        if( n % pvalues[i] == 0 ) { // n is multiple of prime[i]
            // So, we continue dividing n by prime[i] as long as possible
            while( n % pvalues[i] == 0 ) {
                n /= pvalues[i]; // we have divided n by prime[i]
                List[listSize] = pvalues[i]; // added the ith prime in the list
                listSize++; // added a prime, so, size should be increased
            }
            // we can add some optimization by updating sqrtN here, since n
            // is decreased. think why it's important and how it can be added
        }
    }
    if( n > 1 )
    {
        // n is greater than 1, so we are sure that this n is a prime
        List[listSize] = n; // added n (the prime) in the list
        listSize++; // increased the size of the list
    }
}

int main(void)
{
    prime();    
    printf("\nEnter number to factorize: ");
    while(scanf("%ld",&input),input)
    {
          if(input==1)
                printf("1 = 1\n");
            else if(input==-1)
                printf("-1 = -1 x 1\n");
            else
            {
                primeFactorize( input );

            }
    printf("The number %ld has the following factors: ", input);
    for( long i = 0; i < listSize; i++ ) // traverse the List array
                printf("%ld ", List[i]);
    printf("\n\nEnter number to factorize: ");

    }
    return 0;
}

我测试了几个不同的输入:

Enter number to factorize: 81
The number 81 has the following factors: 3 3 3 3 

Enter number to factorize: 123
The number 123 has the following factors: 3 41 

Enter number to factorize: 64
The number 64 has the following factors: 2 2 2 2 2 2 

Enter number to factorize: 0

(exits)

完全符合预期。

答案 1 :(得分:1)

void prime(void){
    long i,j;
    p[0]=1;
    p[1]=1;
    for(i=2;i<=(long)sqrt((double)SIZE);i++)
        if(p[i]==0)
            for(j=2;j*i<SIZE;j++)//<=SIZE ---> <SIZE
                p[i*j]=1;
}

void primeFactorize( long n ){
    listSize = 0;
    long sqrtN = (long)sqrt((double)n);//
    for( long i = 2; i <= sqrtN; i++ ) { //i = 0 ---> i = 2 and p[i] ---> i (Same below)
        while( n % i == 0 ) {//possible [if statement] is omitted
            n /= i;//!
            List[listSize] = i;//!
            listSize++;
        }
    }
    if( n > 1 )
    {
        List[listSize] = n;
        listSize++;
    }
}