如何改进这段C代码-关于素数差距的代码战争问题

如何改进这段C代码-关于素数差距的代码战争问题,c,primes,C,Primes,我目前正在学习C语言,最近一直在练习代码战。我在prime gaps上遇到了这个问题,并对如何改进它感到好奇。起初我被愚弄了,以为这不会那么糟糕,但我意识到寻找素数是困难的(尤其是对于大数来说,它至少是一个NP难问题)。我知道我的代码现在有多个for循环,这在性能方面非常糟糕。我也不完全了解编写C的干净方法,因此我可能没有这样做(例如,我知道释放动态分配的内存是我的责任,但我尝试在main()中释放内存)调用函数并释放所分配内存块的第一个元素——不确定这是否是释放内存块的适当方式) 通常,mai

我目前正在学习C语言,最近一直在练习代码战。我在prime gaps上遇到了这个问题,并对如何改进它感到好奇。起初我被愚弄了,以为这不会那么糟糕,但我意识到寻找素数是困难的(尤其是对于大数来说,它至少是一个NP难问题)。我知道我的代码现在有多个for循环,这在性能方面非常糟糕。我也不完全了解编写C的干净方法,因此我可能没有这样做(例如,我知道释放动态分配的内存是我的责任,但我尝试在main()中释放内存)调用函数并释放所分配内存块的第一个元素——不确定这是否是释放内存块的适当方式)

通常,main函数多次调用prime_gap函数。我知道这段代码之所以有效是因为它被成功提交了,但是有没有更好地编写这段代码的技巧(用C语言算法)

/*长度为“n”的素数间隔表示两个素数之间存在n-1个连续的复合数。
*例如,(2,3)之间的间隙为1,(5,7)之间的间隙为2,(7,11)之间的间隙为4。
*我们的函数应该返回第一对素数,它们满足我们在两个数字之间搜索时寻找的间隔/
在发现的前两个素数的间隙内也不应该存在素数。
*间隙(g,n,m)->其中g=间隙长度,n=搜索空间的开始,m=搜索空间的结束
*/
#包括
#包括
#包括
#包括
长-长*间隙(整数g、整数n、整数m);
布尔校验素数(int,bool);
int main(int argc,const char*argv[]{
长*长*检查3=间隙(2100110);
对于(int i=0;i<2;i++){
printf(“%lld”,勾选3[i]);
}
免费(&check3[0]);
printf(“\n”);
长*检查=间隙(2,3,50);
对于(int i=0;i<2;i++){
printf(“%lld”,勾选[i]);
}
printf(“\n”);
免费(&检查[0]);
长*长*检查1=间隙(2,5,5);
对于(int i=0;i<2;i++){
printf(“%lld”,勾选1[i]);
}
免费(&check1[0]);
printf(“\n”);
长*长*检查2=间隙(4130200);
对于(int i=0;i<2;i++){
printf(“%lld”,勾选2[i]);
}
免费(&check2[0]);
printf(“\n”);
long long*check4=间隙(6100110);
对于(int i=0;i<2;i++){
printf(“%lld”,勾选4[i]);
}
免费(&check4[0]);
printf(“\n”);
长-长*间隙(整数g、整数n、整数m){
long long*result=(long long*)malloc(sizeof(long long)*2);//为整数数组动态分配2个long long
如果(结果==NULL){
perror(“内存不足”);
}
int检验=0;
静态布尔素数;
对于(int i=n;i对于(int j=2;j阅读您的代码,您会想到以下注释:

  • 您永远不会释放malloc分配的空间
    • 因此我想知道你是否真的需要使用malloc,一个简单的全局变量就足够了
  • 检查函数是否有第二个从未使用过的参数prime
  • 在函数间隙中,变量prime表示为static,这不是必需的,也可能导致错误
  • 从算法的角度来看:
    • 你的逻辑是这样的
  • 在全球范围内,如果同一个数字是素数,则需要多次检查,因为这是迄今为止最“昂贵”的操作,所以您应该尽量不要这样做
  • 具体来说,在迭代范围
    i
    中的所有数字之前,应该先检查
    test
    test

codereview.stackexchange.com可能更合适。当然,您可以使用埃拉托斯烯的筛子,降低寻找素数的复杂性,而不影响
malloc()的结果。
/* a prime gap of length "n" indicates that n-1 consecutive composite numbers exist between two primes. 
 * For example, the gap beween (2,3) is 1, the gap between (5,7) is 2 and the gap between (7,11) is 4. 
 * Our function should return the first pair of primes that satisfies the gap that we're looking for in a search between two numbers. /


There should also be no primes that exist within the gap of the first two primes that are found. 
 * gap(g, n, m) -> where g = gap length, n = start of search space, m = end of search space 
 */

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

long long *gap(int g, int n, int m);
bool check_prime(int, bool);

int main(int argc, const char *argv[]){
        long long *check3 = gap(2,100,110);
        for (int i = 0; i < 2; i++){
                printf("%lld ", check3[i]);
        }
        free(&check3[0]);

        printf("\n");

        long long *check = gap(2,3,50);
        for (int i = 0; i< 2; i++){
                printf("%lld ", check[i]);
        }
        printf("\n");
        free(&check[0]);

        long long *check1 = gap(2,5,5);
        for (int i = 0; i < 2; i++){
                printf("%lld ", check1[i]);
        }
        free(&check1[0]);
        printf("\n");

        long long *check2 = gap(4,130,200);
        for (int i = 0; i < 2; i++){
                printf("%lld ", check2[i]);
        }
        free(&check2[0]);
        printf("\n");

        long long *check4 = gap(6,100,110);
        for (int i = 0; i < 2; i++){
                printf("%lld ", check4[i]);
        }
        free(&check4[0]);
        printf("\n");

 long long *gap(int g, int n, int m) {

        long long *result = (long long*) malloc(sizeof(long long) *2); // dynamically allocate 2 long longs for the integer array 
        if (result == NULL){
                perror("Not enough memory");
        }
        int test = 0;
        static bool prime;

        for (int i = n; i < m; i++) {  // traverse search space 
                prime = true;
                prime = check_prime(i, prime);
                if (prime == true) { // identifies prime number
                        test = i + g; // add the gap value to identified prime 
                        prime = false; // set bool to false to now check for any primes that exist between i and i+gap 
                        for (int z = i+1; z < test; z++ ) {    // check there is no prime in between the first and second (test) primes 
                                prime = check_prime(z, prime);
                                if (prime == true) break;
                        }
                        if (prime != true) {   // found no primes between i and i+gap
                                prime = true; // set bool to true to then toggle off in the check right below if i+gap is not actually prime
                                prime = check_prime(test, prime);   // now need to check whether i+gap itself is a prime
                                if (prime == true) {
                                        result[0] = i; result[1] = test;
                                        return result;
                                }
                        }
                }
        }
        result[0] = result[1] = 0;
        return result;
}

bool check_prime(int i, bool prime){
        for (int j = 2; j <= sqrt(i); j++){
                if (i % j == 0) {
                        return false;
                }
        }
        return true;
}
for i in range to check:
    if i is prime
      check if all the number between i and i+gap are not prime
      if i+gap is prime then return the tuple(i, i+gap)