Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 选择一个程序来计算素数对的数目,其和等于一个数N(N_C_Optimization_Numbers - Fatal编程技术网

C 选择一个程序来计算素数对的数目,其和等于一个数N(N

C 选择一个程序来计算素数对的数目,其和等于一个数N(N,c,optimization,numbers,C,Optimization,Numbers,我在下面写了一段代码,要求用户输入一个数n3

我在下面写了一段代码,要求用户输入一个数n3
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void seleciona_primos (int *vet, int n, int raiz){
    int i, j;
        vet[2] = 2;
    for(i=3; i<=n; i+=2){
        vet[i] = i;
    }
    for (i=3; i<= raiz; i+=2){
        if(vet[i]!=0){
            for(j=3; j<=n; j+=2){
                if ((vet[i]!=vet[j]) && (vet[j]%vet[i] == 0)){
                        vet[j]=0;
                }
            }
        }
    }
}

int conta_pares (int *vet, int n){
    int i, j, count=0;
    for (i=3; i<=n; i+=2){
        if(vet[i]!=0){
            for(j=3; j<=n/2; j+=2){
                if((vet[j]!=0) && (vet[i] + vet[j] == n)&& (vet[i]!=0)){
                    //printf("%d ", vet[i]);
                    //printf("%d\n", vet[j]);
                    count++;
                    vet[i] = 0;
                }
            }
        }
    }

    if(vet[n-2]!=0){
        count++;
        }

    return count;
}

int main()
{
    int *vet, n, raiz, i , count;

    scanf("%d", &n);

    vet = (int *) calloc((n+1), sizeof(int));

    raiz =  floor(sqrt(n));

    seleciona_primos(vet, n, raiz);

    count = conta_pares(vet, n);

    printf("%d",count);
    //for(i=3; i<=n; i+=2){
            //if(vet[i]!=0){
                //printf("%d\n", vet[i]);
            //}

    //}
    return 0;
}


我制作一个数组,从2到N1对素数进行计数,它只有一个除数,0有无穷多个除数,然后我检查数组中的两个数是否等于N

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool Prime(int);

int main()
{
long N;
do
{
   printf("Give me the number of numbers :");
   scanf("%ld",&N);
}while(N<=4||N>1000000);//N must be between 3 & 1000000
int prime[N];
int arr[N];
int j=0;

for(int i=2;i<N;i++)
{
   if(Prime(i)==true)
   {
      prime[j]=i;
      j++;
   }
}
printf("\n\n");
for(int p=0;p<j-1;p++)
{
    for(int q=p+1;q<j;q++)
    {
         if(N==prime[p]+prime[q])
         {
              printf("\n%d = %d + %d \n",N,prime[p],prime[q]);
         }
    }
}
printf("\n\n");
return 0;

}


bool Prime(int n)
{
    for(int i=2;i<=(n/2);i++)
    {
        if(n%i==0)
        {
            return false;
        }
    }
    return true;
}
例如:

输入:N=100

输出:

100=3+97

100=11+89

100=17+83

100=29+71

100=41+59


100=47+53

我制作一个数组,从2到N1计算素数,它只有一个除数,0有无穷多个除数,然后我检查数组中的两个数是否等于N

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool Prime(int);

int main()
{
long N;
do
{
   printf("Give me the number of numbers :");
   scanf("%ld",&N);
}while(N<=4||N>1000000);//N must be between 3 & 1000000
int prime[N];
int arr[N];
int j=0;

for(int i=2;i<N;i++)
{
   if(Prime(i)==true)
   {
      prime[j]=i;
      j++;
   }
}
printf("\n\n");
for(int p=0;p<j-1;p++)
{
    for(int q=p+1;q<j;q++)
    {
         if(N==prime[p]+prime[q])
         {
              printf("\n%d = %d + %d \n",N,prime[p],prime[q]);
         }
    }
}
printf("\n\n");
return 0;

}


bool Prime(int n)
{
    for(int i=2;i<=(n/2);i++)
    {
        if(n%i==0)
        {
            return false;
        }
    }
    return true;
}
例如:

输入:N=100

输出:

100=3+97

100=11+89

100=17+83

100=29+71

100=41+59


100=47+53

谢谢你的建议!! 经过一些研究和尝试,我找到了如下解决方案:

int isPrime(int x)
{
    int i;
    for(i = 2; i <= sqrt(x); i++)
    {
        if(x%i == 0)
        {
            return 0;
        }
    }
    return 1;
}


int main()
{

   int i, count = 0, n ;

    scanf("%d", &n);
    for(i = 3; i <= n/2 ; i+=2)
    {
        if(isPrime(i))
        {
            if((isPrime(n-i)))
            {
                count++;
            }
        }
    }


    if(isPrime(n-2))
    {

    count++;

    }

    printf("%d", count);

    return 0;
}

谢谢你的建议!! 经过一些研究和尝试,我找到了如下解决方案:

int isPrime(int x)
{
    int i;
    for(i = 2; i <= sqrt(x); i++)
    {
        if(x%i == 0)
        {
            return 0;
        }
    }
    return 1;
}


int main()
{

   int i, count = 0, n ;

    scanf("%d", &n);
    for(i = 3; i <= n/2 ; i+=2)
    {
        if(isPrime(i))
        {
            if((isPrime(n-i)))
            {
                count++;
            }
        }
    }


    if(isPrime(n-2))
    {

    count++;

    }

    printf("%d", count);

    return 0;
}

我修改了你的代码来优化它,我设置了两个布尔向量,一个用于5个mod6素数vet1[I]=false,对应于素数6xi-1es。i=1对应于质数5=6x1-1,一个vet2[i]对应于质数6x1+1,对于质数1 mod6

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
void seleciona_primos (bool *vet1, bool *vet2,int n){
    int i, i1,imax;   
    imax=(n-n%6)/6+1;
    for (i=1; (6*i-1)*(6*i-1) <=n; i++){
        if (vet1[i]==false){
            for(int i1 = 6*i*i; i1 <= imax+2*i; i1 += (6*i-1)){
                if(i1<imax)
                   vet1[i1]=true;
                vet2[i1-2*i]=true;        
             }
        }
        if (vet2[i]==false){
            for(int i1 = 6*i*i; i1 <= imax; i1 += (6*i+1)){
                vet1[i1]=true;
                if(i1<imax-2*i)
                    vet2[i1+2*i]=true;        
             }
        }
    }
}

int conta_pares (bool *vet1, bool *vet2, int n){
    int i,imax, count=0,r6;
    imax=(n-n%6)/6;
    r6=n%6;
    if (r6==0){
        for (i=1; i<imax; i++){
                if(vet1[i]== false && vet2[imax-i]== false)
                    count++;
         }
    }
    if (r6==2){
            for (i=1; i<imax; i++){
              if(vet2[i]== false && vet2[imax-i]== false)
                 count++;
            }
            count=(count+count%2)/2;
    }
    if (r6==4){
        for (i=1; i<=imax; i++){
          if(vet1[i]== false && vet1[imax+1-i]== false)
                 count++;
        }
        count=(count+count%2)/2;
    }
    if (r6>0 && r6<3){
        if (vet1[imax]==false)
        count++;  
    }
    if (r6>2 && r6<5){
        if (vet2[imax]==false)
    count++;   
    }
    return count;
}

int main()
{
    int  n, i , count;
    bool *vet1, *vet2;
    scanf("%d", &n);

    vet1 = (bool *) calloc((n-n%6)/6+2, sizeof(bool));
    vet2 = (bool *) calloc((n-n%6)/6+2, sizeof(bool));


    seleciona_primos(vet1,vet2, n);

    count = conta_pares(vet1,vet2, n);

    printf("%d",count);
    free(vet1);
    free(vet2);
   return 0;
}

我修改了你的代码来优化它,我设置了两个布尔向量,一个用于5个mod6素数vet1[I]=false,对应于素数6xi-1es。i=1对应于质数5=6x1-1,一个vet2[i]对应于质数6x1+1,对于质数1 mod6

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
void seleciona_primos (bool *vet1, bool *vet2,int n){
    int i, i1,imax;   
    imax=(n-n%6)/6+1;
    for (i=1; (6*i-1)*(6*i-1) <=n; i++){
        if (vet1[i]==false){
            for(int i1 = 6*i*i; i1 <= imax+2*i; i1 += (6*i-1)){
                if(i1<imax)
                   vet1[i1]=true;
                vet2[i1-2*i]=true;        
             }
        }
        if (vet2[i]==false){
            for(int i1 = 6*i*i; i1 <= imax; i1 += (6*i+1)){
                vet1[i1]=true;
                if(i1<imax-2*i)
                    vet2[i1+2*i]=true;        
             }
        }
    }
}

int conta_pares (bool *vet1, bool *vet2, int n){
    int i,imax, count=0,r6;
    imax=(n-n%6)/6;
    r6=n%6;
    if (r6==0){
        for (i=1; i<imax; i++){
                if(vet1[i]== false && vet2[imax-i]== false)
                    count++;
         }
    }
    if (r6==2){
            for (i=1; i<imax; i++){
              if(vet2[i]== false && vet2[imax-i]== false)
                 count++;
            }
            count=(count+count%2)/2;
    }
    if (r6==4){
        for (i=1; i<=imax; i++){
          if(vet1[i]== false && vet1[imax+1-i]== false)
                 count++;
        }
        count=(count+count%2)/2;
    }
    if (r6>0 && r6<3){
        if (vet1[imax]==false)
        count++;  
    }
    if (r6>2 && r6<5){
        if (vet2[imax]==false)
    count++;   
    }
    return count;
}

int main()
{
    int  n, i , count;
    bool *vet1, *vet2;
    scanf("%d", &n);

    vet1 = (bool *) calloc((n-n%6)/6+2, sizeof(bool));
    vet2 = (bool *) calloc((n-n%6)/6+2, sizeof(bool));


    seleciona_primos(vet1,vet2, n);

    count = conta_pares(vet1,vet2, n);

    printf("%d",count);
    free(vet1);
    free(vet2);
   return 0;
}

尝试使用这个非常有效的素数算法:这听起来和看起来像是针对某个竞争网站的。它们的问题是,你几乎永远无法使用简单、天真的暴力解决方案,它们都依赖于某种肮脏的把戏。两个奇数只能和一个偶数相加。所以,如果N-2不是素数,就没有解决方案。试着使用这个非常有效的素数算法:这听起来像是针对某个竞争网站的。它们的问题是,你几乎永远无法使用简单、天真的暴力解决方案,它们都依赖于某种肮脏的把戏。两个奇数只能和一个偶数相加。如果N-2不是素数,就没有解,你可以用i进一步优化循环,你可以用i进一步优化循环