【问题标题】:prime factorization using sieve method [closed]使用筛法进行素数分解
【发布时间】:2013-06-09 05:27:46
【问题描述】:

我的代码有什么问题?
我正在尝试找到主要因素,但在输入它后它不起作用。

我现在该怎么办?我在生成素数后使用单独的方法生成素数我只是在 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;
}

【问题讨论】:

  • “我现在该怎么办?” - 打开调试器...
  • 您的代码可能会在prime() 函数中崩溃,因为您循环到j*i&lt;=SIZE,然后访问p[i*j],当j*i==SIZE 时超出范围。您应该使用j*i&lt;SIZE 作为for 循环中的条件。这可能不是您唯一的错误...正如@MitchWheat 建议的那样:使用调试器查找崩溃的行。
  • 如果您从输入1 开始,您从未设置listSize,但您尝试通过循环打印出值。您必须在 primeFactorize 函数之外进行初始化。

标签: c++ c primes prime-factoring


【解决方案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++;
    }
}

【讨论】:

  • 但是老兄@BLUEPIXY 如果我使用 i 为什么要生成 p[i] 。如果使用 p[i] 则不起作用。而不是我。
  • @rakib_cse06 在函数prime(),将p[i] 设置为10。我认为它不能在函数primeFactorize 中使用,这很明显。
  • 如果我使用 p[i] 并再次崩溃,它不起作用
  • 我不知道。它对我来说很好。
  • 这是你的任务。如果提出变量 pvalues[i] 是多余的,所以你想使用 p[i],将 p[i] 设置为 0 或素数(本身)。
【解决方案2】:

在您的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;
  }     
}

当然,您必须正确初始化这些数组,并在primeFactorize 函数中使用pvalues 而不是p。如果你很聪明,你会发现实际上你可以对这两个东西使用相同的数组 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)

完全符合预期。

【讨论】:

  • dude @Will Ness 我用过它,但它没有像 4 那样给我输出,输出将是 2 2 。但它给出了 4 。 void primeFactorize(long n){ listSize = 0; long sqrtN = (long)sqrt((double)n);// for( long i = 2; i i = 2 and p[i] -- -> i (下同) while( n % pvalues[i] == 0 ) {//可能[if语句]被省略 n /= pvalues[i];//! List[listSize] = pvalues[i];//!列表大小++; } } if( n > 1 ) { 列表[listSize] = n;列表大小++; } }
  • 我现在该怎么办?
  • @Floris 是回答你的人;我只是调整了他们答案的格式。 :) your code, formatted:使用i 而不是pvalues[i]List 必须在全局范围内的某个地方定义为 long List[1024] 或较大的大小,但是如果您有超过 1024 个因素,代码将崩溃。是的,对于 4,它会将 List[] 中的两个第一个条目设置为 2,并将 listSize 设置为 2。unsigned listSize=0 也必须在全局范围内的某个位置定义。对于 6,它将设置 List[]={2,3}listSize=2。没关系。
  • 我建议你打印出数组pvalues 来证明它包含一个素数列表(尤其是它包含2)。如果不是,请检查用于生成素数的代码。这是基本的逐步调试。我给了你一个通用的方法,但这不是一个免费的“为我编写和调试我的代码”网站 - 相反,它是一个“我似乎被卡住了;任何人都可以解开我”网站吗?我认为指出您的 p 数组不包含素数(这是您使用它的方式)算作“不粘”。现在你需要做一些工作......
  • @WillNess - 感谢您的编辑。我在手机上完成了此操作,您无法预览格式...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多